VIEWs in Entity-Framework?
I have a table called Item.
I have a view called ItemView, that returns all the column of Item + one more aggregated column, that I want to be read only.
I need to use it in Entity Framework, and I don't know how I should use it, since when insert the view in the designer, all the fields become entity-keys, besides no relationships available, so I can't access the related tables as nav-properties as from the base table.
Is there a way to make them both into one class? what else can I do?
Say I have an entity Item. after saving this Item I want to retrieve its computed-values from the view, how is this don开发者_StackOverflow社区e?
I never used Views in EF, What should be the best-practice used in these scenarios?
Any advice, link, blog, article, joke is welcommed.EF Designer automatically marks every NOT NULL field as part of primary key. You'll have to manually edit edmx file and correct it. When primary key is set properly, you'll have to define relations between table and view yourself. You should read this entry:
Entity Framework: Creating a model using views instead of tables
If you don't want to change edmx file every time you change your model, you can change definition of your view. If field is recognized as not null, you can change view definition from:
select field_name from table_name
to
select coalesce(field_name,null) field_name from table_name
This way field is not recognized as not null and only primary key fields in view are recognized as not null.
I ended up using this extension which solved it all for me:
Frustrated by lack of support for SQL-Views in ADO.NET Entity-Framework Designer?
精彩评论