[英]Having an entity without primary key defined when there is only 1 record
I have an Entity Framework Code First MVC3 solution where I have an entity named Settings where I only have 1 record in it. 我有一个Entity Framework Code First MVC3解决方案,其中有一个名为Settings的实体,其中只有1条记录。 This entity is used to store general settings for the solution.
该实体用于存储解决方案的常规设置。
public class Settings
{
public bool CommunicationActivated { get; set; }
public string CommunicationMessage { get; set; }
...
}
When I run the solution, I got the error: System.Data.Edm.EdmEntityType: : EntityType 'Settings' has no key defined. 运行解决方案时,出现错误: System.Data.Edm.EdmEntityType::EntityType'设置'没有定义键。 Define the key for this EntityType.
定义此EntityType的键。
My question : is it possible to have an entity without key defined? 我的问题 :是否有可能没有定义键的实体? Is it a good idea for an entity with only one record?
对于只有一个记录的实体,这是一个好主意吗?
Thanks. 谢谢。
I recommend that you just add a new column: 我建议您只添加一个新列:
ID INT NOT NULL IDENTITY PRIMARY KEY
This does no harm and allows you to work with the tools, not against them. 这没有害处,并且允许您使用这些工具,而不是针对它们。
You can even ensure that the table only has one record: Set the ID
field manually to 1
and add a check constraint for (ID = 1)
. 您甚至可以确保该表仅包含一条记录:将
ID
字段手动设置为1
并为(ID = 1)
添加检查约束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.