C# 我们可以在实体框架中拥有没有主键的表吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15381233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we have table without primary key in entity framework?
提问by Prateek
采纳答案by Wahid Bitar
No you can't because Entity Framework needs to know the key to keep track on the object when you make an update or delete operation.
不,您不能,因为 Entity Framework 需要知道在您进行更新或删除操作时跟踪对象的密钥。
Anyway it's not a good idea to have a table without a PrimaryKey
无论如何,没有主键的表不是一个好主意
回答by sirpadk
There is a great difference between what EF can do with a database, and what is possible with a database.
EF 可以对数据库执行的操作与可以对数据库执行的操作之间存在很大差异。
Most databases allow for a table to be without a primary key. Most databases also allow for a table to be without a clustered index / Index Organized Table (or what ever it is the specific term for it in other database systems).
大多数数据库允许表没有主键。大多数数据库还允许表没有聚集索引/索引组织表(或其他数据库系统中的特定术语)。
There is nothing wrong with that, and one should not state that it is a bad idea to have a table without a PK.
这并没有错,人们不应该说拥有一张没有 PK 的桌子是一个坏主意。
As always, it dependes on the needs and usage of the specific table. e.g. a log table, does not need a PK. It will never be used as a FK, so what is it use?
与往常一样,这取决于特定表的需求和使用情况。例如日志表,不需要PK。它永远不会被用作 FK,那么它有什么用呢?
Bottom line, EF does not support tables without a key out of the box, there are some weird workarounds, but none that I have seen is good enough. That is a shame.
最重要的是,EF 不支持没有开箱即用键的表,有一些奇怪的解决方法,但我所见过的都不够好。这是一种耻辱。
回答by Sean B
- Entity Framework musthave a key identified on the entity (POCO class) that models the table.
- The key you define in Entity Framework does NOTneed to be present in the underlying database (e.g. sql table).
- 实体框架必须在对表建模的实体(POCO 类)上标识一个键。
- 您在实体框架中定义的关键不NOT需要存在于底层的数据库(如SQL表)。
If your SQL table does not have a primary key, you can still model it in Entity Framework, you will just need to define a key for that Entity. Pick one or more (possibly all) columns on the Entity that, when combined, will uniquely identify that instance within a collection of the entities. Note this is only important for updating or deleting entities, in that they need to be uniquely identified against the others in that collection to target the change. Find/Select and Add/Insert do not require this consistency.
如果您的 SQL 表没有主键,您仍然可以在实体框架中对其进行建模,您只需要为该实体定义一个键。在实体上选择一个或多个(可能是所有)列,这些列在组合时将在实体集合中唯一标识该实例。请注意,这仅对更新或删除实体很重要,因为它们需要针对该集合中的其他实体进行唯一标识以针对更改。查找/选择和添加/插入不需要这种一致性。
回答by Slavik Shynkarenko
Sometimes adding PK is not possible because it can be read-only, super protected database of a client which you have to use. EF throws exception even on SELECT operations. When I faced that issue I was able to solve it like this (names are artificial):
有时添加 PK 是不可能的,因为它可以是您必须使用的客户端的只读、超级保护数据库。即使在 SELECT 操作上,EF 也会抛出异常。当我遇到这个问题时,我能够像这样解决它(名称是人为的):
[Table( "WeirdTable", Schema = "SomeSchema" )]
public class WeirdTable
{
[Column( "ID1" )]
public int Id1 { get; set; }
[Column( "ID2" )]
public int Id2 { get; set; }
}
In the code file of context:
在上下文的代码文件中:
protected override void OnModelCreating( DbModelBuilder model_builder )
{
base.OnModelCreating( model_builder );
model_builder.Entity<WeirdTable>().HasKey(
t => new { t.Id1, t.Id2 }
);
}
So basically you just need to specify which columns can be primary key.
所以基本上你只需要指定哪些列可以是主键。
回答by Jpsy
Starting with .NET Core 2.1, EF Core supports models without primary key.
This is realized through a concept of Query Typesinstead of Enitity Types.
See https://docs.microsoft.com/en-us/ef/core/modeling/query-types
从 .NET Core 2.1 开始,EF Core 支持没有主键的模型。
这是通过查询类型而不是实体类型的概念来实现的。
请参阅https://docs.microsoft.com/en-us/ef/core/modeling/query-types
Some of the main usage scenarios for query types are:
- Serving as the return type for ad hoc FromSql() queries.
- Mapping to database views.
- Mapping to tables that do not have a primary key defined.
- Mapping to queries defined in the model.
查询类型的一些主要使用场景是:
- 用作临时 FromSql() 查询的返回类型。
- 映射到数据库视图。
- 映射到没有定义主键的表。
- 映射到模型中定义的查询。
Query Typeshave limits though. They cannot be inserted, updated or deleted on the database. And they have only limited support of navigation properties.
查询类型虽然有限制。不能在数据库中插入、更新或删除它们。而且它们对导航属性的支持有限。
回答by AndrewSilver
EF.Core 5 will have [Keyless]
attribute allowing to have an entity without PK.
But it is still under development.
EF.Core 5 将具有[Keyless]
允许拥有没有 PK 的实体的属性。但它仍在开发中。