简体   繁体   English

在没有主键的情况下将实体保存在实体框架中

[英]Save entity in Entity Framework without a primary key

Scenario: 场景:

Database first. 数据库优先。 I have a table with no primary key set and I'm trying to make an update with Entity Framework. 我有一个没有主键集的表,我正在尝试使用实体框架进行更新。

This is the error message I keep getting: 这是我不断收到的错误消息:

The property 'inactive_date' is part of the object's key information and cannot be modified. 

If I set the fields 'Entity Key' value to 'false' I get this error messge: 如果我将“实体键”字段的值设置为“假”,则会收到此错误消息:

Modifications to tables where a primary key column has property 'StoreGeneratedPattern' set to 'Computed' are not supported. 不支持对主键列的属性“ StoreGeneratedPattern”设置为“计算”的表进行修改。 Use 'Identity' pattern instead. 请改用“身份”模式。 Key column: 'timestamp'. 关键列:“时间戳记”。 Table: 'plat12Model.Store.glchart'. 表格:“ plat12Model.Store.glchart”。

Would this be corrected if I created a primary key? 如果我创建了主键,是否可以解决? Can I set a primary key in my code rather than on the database? 我可以在代码中而不是数据库中设置主键吗?

By default, EF will make tables without primary keys and views into read-only classes where every field is part of the composite key. 默认情况下,EF将使没有主键和视图的表变成只读类,其中每个字段都是组合键的一部分。 You can modify the conceptual model to reflect the actual behavior as long as you retain a key value that EF will use for object tracking. 您可以修改概念模型以反映实际行为,只要您保留EF将用于对象跟踪的键值即可。 As the error message states, you also need to make the columns no longer computed in order to update them as well. 如错误消息所述,您还需要使列不再计算,以便更新它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM