EF删除数据

ShopEntities db = new ShopEntities();
var customers = from c in db.Customer
where c.ID == 21
select c;
var customer = customers.FirstOrDefault();
if (customer != null)
{
//删除标记
db.Entry(customer).State = System.Data.EntityState.Deleted;//删除标记
db.SaveChanges();
}

ShopEntities db = new ShopEntities();
var customers = from c in db.Customer
where c.ID == 22
select c;
var customer = customers.FirstOrDefault();
if (customer != null)
{
customer.CustomerName = "lisi88888";
db.Entry(customer).State = System.Data.EntityState.Modified;//修改标记
db.SaveChanges();
}
}

Model-First(先创建模型,然后再根据模型创建数据库。如果主键ID是Guid类型在创建模型时,将相应的属性取消自增StoreGeneratedPattern设置为none)

原文地址:https://www.cnblogs.com/poli/p/4297833.html