使用实体框架继承时插入失败
我有两个表,一个名为 Person,它是抽象的,具有标量属性“Id”和“Name”,其中键“Id”是主键。另一个名为“Student”的继承自 Person
我使用以下代码将学生插入数据库:
TestModeContainer testModel = new TestModeContainer();
Student student = testModel.CreateObject<Student>();
student.Id = "s1";
student.Grade = "g2";
student.Name = "Lighter.Spark";
testModel.AddObject("PersonSet", s);
testModel.SaveChanges();
当我调用 SaveChanges 将数据保存到数据库时,发生 System.Data.UpdateException。内部异常显示“无法将 NULL 值插入到表 'Temp.dbo.PersonSet' 的列 'Id' 中;列不允许为空值。INSERT 失败。\r\n该语句已终止。” 谁能告诉我为什么?以及如何纠正这个问题?
I have two tables, the one named Person, which is abstract, with scalar property "Id" and "Name", where the key "Id" is the primary key. another one named "Student" inherit from Person
I use following code to insert a student to db:
TestModeContainer testModel = new TestModeContainer();
Student student = testModel.CreateObject<Student>();
student.Id = "s1";
student.Grade = "g2";
student.Name = "Lighter.Spark";
testModel.AddObject("PersonSet", s);
testModel.SaveChanges();
When I call SaveChanges to save data to database, an System.Data.UpdateException occurred. The inner exception says "Cannot insert the value NULL into column 'Id', table 'Temp.dbo.PersonSet'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."
Who can tell me why? And how to correct this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
Id
字段的StoreGeneratePattern
更改为None
。Identity
意味着将在数据库中设置值,并且由于应用程序中设置的值永远不会传递到数据库。Change the
StoreGeneratedPattern
for yourId
field toNone
.Identity
means that value will be set in the database and because of the the value set in the application is never passed to the database.