简体   繁体   English

实体框架 - 从视图创建模型?

[英]Entity Framework - Create a model from a view?

I'm brand new to ASPNET and MVC 4, so I imagine that this may be a easy question. 我是ASPNET和MVC 4的新手,所以我想这可能是一个简单的问题。 However, I've been unable to properly google an answer. 但是,我一直无法正确谷歌答案。 I would simply like to display some sales information - I only need to display it. 我只想显示一些销售信息 - 我需要显示它。 I do not need to insert, update, or delete from the underlying tables. 我不需要从基础表插入,更新或删除。

There are 3 SQL Server tables from which data needs to be pulled: CurrentSales, SalesPlans, and AverageSales. 有3个SQL Server表需要从中提取数据:CurrentSales,SalesPlans和AverageSales。 I created a VIEW for this and put a unique clustered index on it; 我为此创建了一个VIEW并在其上放置了一个唯一的聚簇索引; it contains some outer joins but has logic to handle any unlikely NULL values. 它包含一些外连接,但具有处理任何不太可能的NULL值的逻辑。

I go to MODELS, add a new ADO.NET Entity Data Model, and add my view to the model. 我转到MODELS,添加一个新的ADO.NET实体数据模型,并将我的视图添加到模型中。 It comes back and says "The table/view 'vw_FullView' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view." 它返回并说“表/视图'vw_FullView'没有定义主键。已经推断出密钥,并且定义被创建为只读表/视图。” When I create a Controller class from this model and entity, the view displays no data when I load the website. 当我从此模型和实体创建Controller类时,视图在加载网站时不显示任何数据。

However, if I create a completely blank table - with an appropriate primary key - that acts as the model table, then use a stored procedure (as a Function Import) to retrieve the data I need, everything works completely fine. 但是,如果我创建一个完全空白的表 - 使用适当的主键 - 充当模型表, 然后使用存储过程(作为函数导入)来检索我需要的数据,一切正常。

This obviously can't be the correct way to handle this. 这显然不是处理这个问题的正确方法。 Is there a way to create a strongly typed model out of a view? 有没有办法从视图中创建强类型模型? I'd prefer to have the Controller and View objects in ASP.NET properly auto-generate from the SQL view in the model, rather than needing this blank table to "fool" the system. 我更喜欢让ASP.NET中的Controller和View对象从模型中的SQL视图中自动生成,而不是需要这个空白表来“欺骗”系统。

Many thanks in advance. 提前谢谢了。

Entity framework does not like views very much. 实体框架非常不喜欢视图。 It cannot figure out what the primary keys are for a view, so it assumes any non-nullable field is part of a composite primary key. 它无法确定视图的主键是什么,因此它假定任何非可空字段都是复合主键的一部分。

In general, avoid views when using Entity Framework. 通常,在使用Entity Framework时避免使用视图。 Create a linq query that does what your view does and use that instead. 创建一个linq查询,它可以执行您的视图所做的操作并使用它。

a view is just a projection of an entity or a combination of entities. 视图只是实体或实体组合的投影。 by Default entity Framework will only allow read-only-access as there are no real entities in a view, meaning ET can not track changes. 默认实体框架只允许只读访问,因为视图中没有真实实体,这意味着ET无法跟踪更改。

simmdan actually did a good Job explaining how to work around this in the MSDN forum simmdan实际上做了很好的工作,解释了如何在MSDN论坛中解决这个问题

basically, as Mystere Man already pointed out, if you use Entity Framework, the easiest way would be to clone your view using the real entities joined and filtered by linq. 基本上,正如Mystere Man已经指出的,如果你使用实体框架,最简单的方法是使用由linq连接和过滤的真实实体来克隆你的视图。

It's true that Entity Framework does not like SQL Server views because the primary key is not obvious in a view. 确实,Entity Framework不喜欢SQL Server视图,因为主键在视图中并不明显。 Unfortunately, specifying [Key] in the model doesn't seem to be sufficient. 不幸的是,在模型中指定[Key]似乎不够。 However, it can be done and is very useful in the 98% of cases where there's a view that combines tables and you just want to display it's data in a grid. 但是,它可以完成,并且在98%的情况下非常有用,在这种情况下,有一个组合表的视图,你只想在网格中显示它的数据。 The key (pardon the pun) is to define the view correctly. 关键(原谅双关语)是正确定义视图。

My original view contained the following columns: 我的原始视图包含以下列:

DepartmentCode(varchar(8),not null)
DepartmentName(varchar(60), not null)
DivisionCode(varchar(8), null)
DivisionName(varchar(8), null)
StatusCode(char(1), not null)
Virtual(varchar(1), not null)

and in my model I specified 在我指定的模型中

[Key]
[Column("DepartmentCode")]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[StringLength(8)]
[Display(Name = "Department Code")]
public string DepartmentCode { get; set; }

When I scaffolded this and displayed the Index view, I got an error referring to a primary key. 当我搭建这个并显示索引视图时,我在引用主键时出错。 Apparently, Entity Framework assumes all the not null fields contribute to a primary key. 显然,Entity Framework假定所有非空字段都对主键有贡献。

Removing the extra not nulls did the trick. 删除额外的非空值就可以了。 I forced the columns in the view to be nullable using the following: 我使用以下方法强制视图中的列可以为空:

CREATE view [dbo].[Departments] as
select DepartmentCode
,nullif(DepartmentName,'') as DepartmentName
,nullif(DivisionCode,'') as DivisionCode
,nullif(DivisionName,'') as DivisionName
,nullif(StatusCode,'') as StatusCode
,nullif(Virtual,'') as Virtual
from ....

Now the view columns look like this 现在视图列看起来像这样

DepartmentCode(varchar(8),not null)
DepartmentName(varchar(60), null)
DivisionCode(varchar(8), null)
DivisionName(varchar(8), null)
StatusCode(char(1), null)
Virtual(varchar(1), null)

Once I changed the view, the Index view worked correctly. 一旦我更改了视图,索引视图就可以正常工作。

I also confirmed that a view that contains a compound (multi-column) key will also work as long as only those columns are not null and in your model you specify [Key] on each column and also add Order=1 and Order=2 to the [Column] annotations on those key columns. 我还确认包含复合(多列)键的视图也可以工作,只要这些列不为空,并且在模型中为每列指定[Key]并添加Order = 1和Order = 2到那些关键列的[Column]注释。

Of course, the assumption here is that you have permission to modify the view (or can create an alternate view). 当然,这里的假设是您有权修改视图(或者可以创建备用视图)。 I can only confirm this works on MVC 5 and Entity Framework 6. 我只能确认这适用于MVC 5和Entity Framework 6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM