在实体数据模型中定义视图 [英] Defining View in Entity Data Model

查看:136
本文介绍了在实体数据模型中定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"

这篇关于在实体数据模型中定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆