如何将主键添加到sql视图? -或将视图链接到LINQ-2-Entities的替代方法 [英] How do you add a primary key to a sql view? - Or alternate way to link views to LINQ-2-Entities

查看:89
本文介绍了如何将主键添加到sql视图? -或将视图链接到LINQ-2-Entities的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要向我的实体对象模型添加一个非常简单的视图(或尝试添加).该数据库位于SQL Server 2008中.我使用C#在.Net 3.5(SP1)上.

I'm adding a very simple view (or trying to) to my entities object model. The database is in SQL Server 2008. I'm on .Net 3.5 (SP1) using C#.

该视图有两个字段:color和colorcount,分别是Varchar(50)和count(*).

The view has two fields: color and colorcount, a Varchar(50) and a count(*) respectively.

当我从数据库中更新模型并选择要添加的视图时,它将运行(它更新了表,添加字段没有问题),但是没有添加视图.没有错误,警告或消息显示.

When I do Update Model from Database, and select the view to add, it runs (it updated tables, adding fields no problem) but does not add the view. No error, warnings, or messages are displayed.

当我打开.edmx文件时,我看到它显示Warning 6013: No primary key defined.

When I open the .edmx file I see that it shows Warning 6013: No primary key defined.

该视图很复杂,我宁愿不将其转换为LINQ查询.如何添加主键,以便实体支持视图?

The view is complex and I would rather not translate it to a LINQ query. How can I add a primary key so that Entities will support the view?

是否存在将这种视图添加到EDMX的非hacking方式?

Is there a non-hack-around way to add a view like this to an EDMX?

推荐答案

使用schemabinding创建视图之后,可以向其中添加主键:

After you create the view using schemabinding you can add a primary key to it:

CREATE VIEW Colors WITH SCHEMABINDING
AS SELECT Color='yellow', ColorCount=100
GO
CREATE UNIQUE CLUSTERED INDEX PK_Colors ON Colors (Color)

这篇关于如何将主键添加到sql视图? -或将视图链接到LINQ-2-Entities的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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