views:

47

answers:

1

Hello!

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 done?

I never used Views in EF, What should be the best-practice used in these scenarios?
Any advice, link, blog, article, joke is welcommed.

+2  A: 

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.

LukLed