這是我的代碼,很簡單...代碼第一 - 無法將NULL值插入列「ID」
var newUser = new User();
newUser.Id=id;
newUser.Email = email;
this.DataContext.Set<User>().Add(newUser);
this.DataContext.SaveChanges();
我得到的錯誤是在this.DataContext.SaveChanges();
SQLException的指出:
無法將NULL值插入'Id'列中,表 'xxxxx.dbo.Users';列不允許有空值。 INSERT失敗。
我已經調試,發現有在 this.DataContext.Set<User>().Add(newUser);
如果是這樣的話,在NEWUSER標識&現值,又是怎樣成爲價值零?
錯誤堆棧跟蹤:
[DbUpdateException: An error occurred while updating the entries. See the inner exception for details.]
System.Data.Entity.Internal.InternalContext.SaveChanges() +204
System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +23
System.Data.Entity.DbContext.SaveChanges() +20
我一直無法理解或解決這個....
衷心感謝您在這任何幫助......
問候 ARNAB
解決方案
好的,感謝拉迪斯拉夫給我指出了正確的方向: 添加屬性[DatabaseGenerated(DatabaseGeneratedOption.None)]
解決了這個問題。
什麼是id的類型?可能是因爲在數據庫中生成密鑰而出現問題? –
型是SQLSERVER BIGINT,在User類是長.. 此外,當我用正常的實體框架沒有codefirst,它的工作原理 – Arnab
ID參數也長 – Arnab