SQL Server 视图可以有主键和外键吗?
是否可以在 Microsoft SQL Server Management Studio 中定义数据库视图的主键和外键? 如何?
我正在尝试创建一个 ADO.NET 实体数据模型来读取四个我无法修改的旧的、格式不正确的数据库表。 我已经创建了我需要的数据的视图。
四个视图应映射到具有多对多关系的简单三实体 EDMX。
创建数据模型时出现此错误:
表/视图“...”没有 主键已定义但无效 可以推断出主键。 这 表/视图已被排除。 使用 您需要审查的实体 您的架构,添加正确的键并 取消注释。
它正确推断了两个视图的主键。 但与另外两个人却未能做到这一点。
我的问题视图之一使用聚合函数:
SELECT MAX(...) ... GROUP BY ...
另一个应该具有两个外键的复合主键。
Is it possible to define primary and foreign keys for database Views in Microsoft SQL Server Management Studio? How?
I'm trying to create an ADO.NET Entity Data Model to read from four old, poorly-formed database tables that I cannot modify. I've created views of just the data I need.
The four views should map to a simple three-entity EDMX with one many-to-many relationship.
I get this error when creating my Data Model:
The table/view '...' 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.
It correctly inferred the primary keys of two views. But failed to do so with the other two.
One of my problem views uses aggregate functions:
SELECT MAX(...) ... GROUP BY ...
The other ought to have a compound primary key of two foreign keys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要定义视图,以便它:
PRIMARY KEY
列JOIN
UNION's
视图中的任何行都应该映射到表中的一行。
它无法更新。 对于只读实体,来自 此处:
对于第二个问题:
来自 文档< /强>:
You need to define your view so that it:
PRIMARY KEY
columnsJOIN
'sUNION
'sAny row from your view should map to exactly one row from the table.
It cannot be updateable. For a readonly entity, a solution from here:
For the second question:
From documentation:
您可以通过在视图中创建一个
NOT NULL
索引列来更改视图,执行如下操作:You can alter your views by creating a
NOT NULL
index column in your view doing something like this:实际上,您可以创建一个使用 JOIN 的视图,并从中生成模型中的实体。
Actually, you can create a view that uses JOIN's and generate and Entity in your model from it.