本文介绍了如何将模型与数据库等同起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你如何等同:
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; }
}
推荐答案
public ActionResult Delete(int testId)
{
var table1 = new Table1() { testID = testId };
db.Entry(table1).State=EntityState.Deleted;
db.SaveChanges();
这篇关于如何将模型与数据库等同起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!