3

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

Community
  • 1
  • 1
peter
  • 83
  • 6

1 Answers1

8

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.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670