在实体数据模型中定义视图

编程入门 行业动态 更新时间:2024-10-26 08:32:40
本文介绍了在实体数据模型中定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试添加一个实体数据模型的视图,但是我收到以下错误。 视图是一个由一个数组组成的组。我不明白这一点,因为一个视图没有一个主键的性质。

I am trying to add a view to an entity data model but I get the error below. The view is a group by with a count. I don’t understand this because a view does not have a primary key by it’s nature.

我修改了原始帖子,因为我想出了如何添加一个键到视图。但是我仍然有同样的问题。

I modified the original post because I figured out how to add a key to the view. But I still have the same problem.

警告6013:表/视图'fmcsa.dbo.vieFMCSADocumentCount' b $ b没有定义主键,不能推断出有效的主键。 此表/视图已被排除。对于使用实体,您将需要检查您的模式,添加正确的键,并取消注释。

warning 6013: The table/view 'fmcsa.dbo.vieFMCSADocumentCount' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it.

这是查看

CREATE VIEW [dbo]。[vieFMCSADocumentCount] with SCHEMABINDING

CREATE VIEW [dbo].[vieFMCSADocumentCount] with SCHEMABINDING

AS SELECT COUNT_BIG(*) AS CountOfDocs, ROLE_ID, OWNER_ID FROM dbo.FMCSA_DOCUMENT GROUP BY ROLE_ID, OWNER_ID

然后我可以添加一个键

CREATE UNIQUE CLUSTERED INDEX [MainIndex] ON [dbo].[vieFMCSADocumentCount] ( [OWNER_ID] ASC, [ROLE_ID] ASC )

仍然不能工作。

推荐答案

您没有指定,但我假设您正在使用EF4。我曾经遇到过这样的情况 - 您要么手动定义一个密钥,要么编辑重新创建您的视图 WITH SCHEMABINDING 并重新导入。

You didn't specify, but I'm assuming you're using EF4. I've come across this before--you either want to define a key manually or edit recreate your view WITH SCHEMABINDING and reimport.

模式绑定有效地告诉SQL跟踪视图的依赖关系。这是一个祝福和诅咒(尝试在 FMCSA_DOCUMENT 中添加列,一旦此视图具有模式绑定),因此您可能需要阅读效果。

Schema binding effectively tells SQL to track dependencies for your view. It's both a blessing and a curse (try adding a column to FMCSA_DOCUMENT once this view has schema binding), so you might want to read up on the effects.

CREATE VIEW [dbo].[vieFMCSADocumentCount] WITH SCHEMABINDING AS SELECT COUNT(ID) AS CountOfDocs, ROLE_ID, OWNER_ID FROM dbo.FMCSA_DOCUMENT GROUP BY ROLE_ID, OWNER_ID

另外,在EF模型浏览器转到实体类型文件夹,找到您的视图(右键单击并选择在设计器中显示)。然后在视图中,突出显示构成您的主键的列,然后右键单击并选择实体密钥

Alternately, in the EF Model Browser Go to the Entity Types folder, find your view (right click and choose Show in Designer). Then on the view, highlight the column(s) that comprise your primary key and right click and choose "Entity Key"

更多推荐

在实体数据模型中定义视图

本文发布于:2023-11-04 05:48:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557111.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   实体   定义   数据模型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!