我遇到问题产生的原因:数据库表的某个字段为不能为空。在修改实体属性的时候,实体对应的表中不能为空的字段为null。
详情:
数据库:
c #:
错误代码:
static void EditAdvance() { Category c = new Category() { CategoryID = 11,Description = "bad" }; System.Data.Entity.Infrastructure.DbEntityEntry<Category> a = db.Entry<Category>(c); a.State = System.Data.EntityState.Unchanged; a.Property("Description").IsModified = true; db.SaveChanges(); Console.WriteLine("modify success"); }
正确代码:
static void EditAdvance() { Category c = new Category() { CategoryID = 11, CategoryName="new",Description = "bad" }; System.Data.Entity.Infrastructure.DbEntityEntry<Category> a = db.Entry<Category>(c); a.State = System.Data.EntityState.Unchanged; a.Property("Description").IsModified = true; db.SaveChanges(); Console.WriteLine("modify success"); }