如何将模型与数据库等同起来 [英] How do I equate a Model with a Database

查看:65
本文介绍了如何将模型与数据库等同起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何等同:

How do you equate:

db.Data1 = del.thistable;???



这有效:


This works:

del.thistable= db.Data1.ToList();



你怎么做反向?


How do you do the reverse?

public class HomeController : Controller
{
    private testContext db = new testContext();
    private DeleteModel del = new DeleteModel();
    public ActionResult Delete(int testID)
    {
        del.thistable= db.Data1.ToList();
        del.thistable.Remove(del.thistable.SingleOrDefault(o => o.testID == testID));
        db.Data1 = del.thistable;???
    }
}



Class1.cs


Class1.cs

public class Table1
   {
       [Key]
       public int testID { get; set; }
       public string datetime { get; set; }
       public string col1 { get; set; }
       public string col2 { get; set; }
       public string col3 { get; set; }
   }
   public class DeleteModel
   {
       public string errorcode { get; set; }
       public List<Table1> thistable { get; set; }
   }
   public class testContext : DbContext
   {
       public DbSet<Table1> Data1 { get; set; }
   }

推荐答案

尝试更换删除方法如下,你应该好好去。

Try replacing your Delete method with the following and you should be good to go.

public ActionResult Delete(int testId)
   {
            var table1 = new Table1() { testID = testId };
           db.Entry(table1).State=EntityState.Deleted;

              db.SaveChanges();


这篇关于如何将模型与数据库等同起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆