我遇到問題產生的原因:數據庫表的某個字段爲不能爲空。在修改實體屬性的時候,實體對應的表中不能爲空的字段爲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"); }