使用实体框架继承时插入失败

发布于 2024-11-18 22:43:01 字数 586 浏览 8 评论 0原文

我有两个表,一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-11-25 22:43:01

Id 字段的 StoreGeneratePattern 更改为 NoneIdentity 意味着将在数据库中设置值,并且由于应用程序中设置的值永远不会传递到数据库。

Change the StoreGeneratedPattern for your Id field to None. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文