.net 视图中的实体框架主键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1019865/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:53:19  来源:igfitidea点击:

Entity framework primary keys in Views

.netentity-frameworkprimary-keysql-view

提问by Mark

I have to use a View with the EF but, when I import it, the primary key of the view is displayed incorrectly and for some reason I can't change it.

我必须将视图与 EF 一起使用,但是当我导入它时,视图的主键显示不正确,并且由于某种原因我无法更改它。

回答by marc_s

Appears to be a known issue - see this other StackOverflow question and its answers:

似乎是一个已知问题 - 请参阅其他 StackOverflow 问题及其答案:

Entity Framework and SQL Server View

实体框架和 SQL Server 视图

Marc

马克

回答by Mark

I had a similar problem when i wanted to do a group by in a view that was not easy in linq but ended up not working. What worked for me was to specifically change the entity key in Entity Framework designer to a int column that is unique. If it doesnt exist then create a simple incremented int field in your view definition and use that as your key.

当我想在 linq 中并不容易但最终无法工作的视图中进行分组时,我遇到了类似的问题。对我有用的是专门将实体框架设计器中的实体键更改为唯一的 int 列。如果它不存在,则在您的视图定义中创建一个简单的增量 int 字段并将其用作您的键。

回答by Davide Icardi

In my experience the entity keys on a View are defined using the primary keys of the view (from the select clause).

根据我的经验,视图上的实体键是使用视图的主键(来自 select 子句)定义的。

If you have multiple tables in the view, you must select the primary key, not the foreign key, to see it as an entity key.

如果视图中有多个表,则必须选择主键,而不是外键,才能将其视为实体键。