對一個或多個實體的驗證失敗。有關詳細信息,請參見“EntityValidationErrors”屬性。

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章