旧数据库混合主键和外键:更新错误“该属性是对象关键信息的一部分,无法修改”
考虑下面的表:
create table master
( nr number(10,0) identity primary key
lastmodified date
scn number(12,0) -- update from sequence with every update
)
create table details
( nr number(10,0) primary key
linenum number(3,0) primary key
<more details>
scn number(12,0) -- update from sequence with every update
)
表详细信息有一个从第 nr 列到第 nr 列的外键。两个表都映射到以主数据库键作为实体键的实体。 Master和Detail之间是一对多的关系。
当我将具有单个详细信息的主数据添加到 EF 上下文并调用 SaveChanges 时,主数据中的 nr 字段由数据库中的触发器确定,返回到 EF,写入details.nr 属性并保存在详细信息表中。干得好,EF!
但是,当我尝试更新详细信息实体中的详细信息字段之一并调用 SaveChanges 时,我收到错误“属性‘Nr’是对象关键信息的一部分,无法修改。”显然 EF 尝试从相关 Master 更新 Nr 字段。当然,这没有被修改。
解决此限制的最佳方法是什么?添加新的实体键字段并更改主键列不是一个选项。
免责声明:我手动编写了代码来关注该问题,它可能无法编译,但我希望您明白这一点。
更新:我认为(希望...)问题已解决。如果我将详细信息实体中的 SCN 字段设置为 ConcurrencyMode=Fixed 和 StoreGeneratePattern=Compulated,则会显示有关 Nr 字段的错误消息。如果我将 SCN 字段设置为 ConcurrencyMode=Fixed 和 StoreGeneratePattern=None,则 SaveChanges 返回时不会出现错误。
Consider the following tables:
create table master
( nr number(10,0) identity primary key
lastmodified date
scn number(12,0) -- update from sequence with every update
)
create table details
( nr number(10,0) primary key
linenum number(3,0) primary key
<more details>
scn number(12,0) -- update from sequence with every update
)
Table Details has a foreign key from column nr to column nr. Both tables are mapped to entities with the primary database keys as entity keys. There is a one-to-many relation between Master and Detail.
When I add a master with a single detail to the EF context and call SaveChanges, the nr field in the master is determined by a trigger in the database, returned to EF, written into the details.nr property and saved in the details table. Well done, EF!
But when I try to update one of the details fields in the details entity adn call SaveChanges, I get the error "The property 'Nr' is part of the object's key information and cannot be modified." Apparently EF tries to update the Nr field from the related Master. Which is not modified, of course.
What is the best way to work around this limitation? Adding a new entity key field and changing the primary key columns is not an option.
Disclaimer: I wrote the code by hand to focus on the issue, it may not compile but I hope you get the point.
Update: I think (hope...) that the problem is solved. If I set the SCN field in the Detail entity to both ConcurrencyMode=Fixed AND StoreGeneratedPattern=Computed, the error message about the Nr field appears. If I set the SCN fields to ConcurrencyMode=Fixed and StoreGeneratedPattern=None, the SaveChanges returns without errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论