这是我的代码,很简单...代码第一 - 无法将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