简体   繁体   中英

Entity Framework : How could it be possible to add to SQL Server View?

Maybe I miss something here but I don't think so.

I have an entity model and it holds sql server views as well. When I was working with my entity, I noticed something like following method;

HubEntities _entities = new HubEntities();
_entities.vw_AccommPropertiesFullWeb.AddObject(...

vw_AccommPropertiesFullWeb here is a view.

How can it be possible to add a new view to a view? Does EF guys did it on purpose or it is something which is misconfigured?

If you import SQL View to the model it is always represented as DefiningQuery which is by definition read only. The only way to make it writable is implementing stored procedures and map them to Insert , Update and Delete operations of the entity type.

The workaround is mapping view in the way that EF will believe that it is a table. It requires:

  • Manual modification of EDMX file and unless you buy some additional tool you will also never be able to use update from database again = you are going to maintain EDMX manually
  • Your view must be updatable - it must follow some rules defined by database server - generally when using with EF it either means that your view must have INSTEAD OF triggers for modification operations or it cannot use joins, union, computed columns, aggregations or anything else unless columns resulting from these operations are marked as computed in EF (database views allows inserting and updating columns only from base table).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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