简体   繁体   English

当只有1条记录时,定义没有主键的实体

[英]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.

相关问题 在Entity Framework中使用没有主键的只读表 - Working with a Read Only table without a primary key in Entity Framework 实体类型需要定义一个主键 - Entity Type requires a primary key to be defined 如何在实体框架中创建新记录而不在其参数中指定主键? - How to create a new record in Entity Framework without specifying the primary key in its parameters? 为实体设置主键而不影响数据库 - Set a Primary Key for an Entity without ingaffect database 使用在多个列上具有主键的实体框架更新数据库 - Update Database using Entity Framework having primary key on multiple Columns 传递的主键值的数量必须与实体上定义的主键值的数量相匹配 - The number of primary key values passed must match number of primary key values defined on the entity 如何根据主键查看唯一一条记录? - how to view the only one record based on primary key? 在没有主键的情况下使用 Find 在 dbSet 中查找记录 - Find a record in dbSet using Find without a primary key “传递的主键值的数量必须与实体上定义的主键值的数量匹配。 参数名称:keyValues”实体框架 - “The number of primary key values passed must match number of primary key values defined on the entity. Parameter name: keyValues” Entity Framework 如何在没有主键实体框架的情况下映射表 - How to map table without primary key Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM