EF Code First and Database Views

那年仲夏 提交于 2019-12-10 18:17:42

问题


I just started looking at Database Views with Code First... and try to decide if I should use them.

Here Ladislav recommends to use NotMapped inheritance parent for table and Db-View (my view only adds sums of child entities)... but how this work with CF Migrations? I really want to use them.

Also... navigation properties will work on Db-View Entity?

Is there any way to save data directly into Db-View entity (and it's table)?


回答1:


If you want to use code first and migrations you should not use views. Views are database "logic" constructs and code first is not an approach for creating database logic. With code first you should use the projection which is also mentioned in the linked answer.

Migrations will not be able to detect changes related to your views. You will have to write all migration code for views manually.

If you want to use views you should do database first (= no migrations) and either map them with EDMX or code mapping.

Also... navigation properties will work on Db-View Entity?

This is the only scenario where code mapping provides better support than EDMX. You can define relation in your model even if it doesn't exist in the database (but your database must ensure data integrity). It is in theory possible with EDMX as well but it requires changing EDMX manually.

Is there any way to save data directly into Db-View entity (and it's table)?

Yes but your view must be updatable. I don't think that view with aggregation values is updatable.



来源:https://stackoverflow.com/questions/11974384/ef-code-first-and-database-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!