如何向sql视图添加主键?-或将视图链接到linq -2实体的替代方法

时间:2021-08-28 09:08:17

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#.

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

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

视图有两个字段:颜色和颜色计数、Varchar(50)和count(*)。

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.

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

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

当我打开.edmx文件时,我看到它显示了警告6013:没有主键定义。

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?

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

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

有没有一种非破解的方法可以将这样的视图添加到EDMX中?

2 个解决方案

#1


23  

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

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

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

#2


1  

I dont know if this will help you, but you can create a primary key using a multi-statement table-valued function.

我不知道这是否对您有帮助,但是您可以使用多语句表值函数创建主键。

I cant find any ref to a primary key for a view, but i know it can be done with a table function.

我找不到任何指向视图主键的引用,但我知道它可以通过表函数来完成。

CREATE FUNCTION (Transact-SQL)

创建函数(transact - sql)

#1


23  

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

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

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

#2


1  

I dont know if this will help you, but you can create a primary key using a multi-statement table-valued function.

我不知道这是否对您有帮助,但是您可以使用多语句表值函数创建主键。

I cant find any ref to a primary key for a view, but i know it can be done with a table function.

我找不到任何指向视图主键的引用,但我知道它可以通过表函数来完成。

CREATE FUNCTION (Transact-SQL)

创建函数(transact - sql)