How to determine database view using Entity Framework 4 Code-First?

How to determine database view using Entity Framework 4 Code-First? I can not find anything about it!

+18
ef-code-first
May 04 '11 at 21:09
source share
2 answers

This is because you cannot define a database view using a code-based approach. A database view is a database design that uses SQL Query on top of existing tables / functions. You cannot define such constructs using the first code.

If you want to view, you must create it manually by running a CREATE VIEW SQL script, for example, in a custom initializer - this will be similar to this answer . Just keep in mind that this will not help you if you want to map an object to a view. In this case, you probably have to drop the table created by EF first and create a view with the same name (I have not tried, but could do the trick). Also keep in mind that not every view is udpatable, so you are likely to get only a read-only object.

+17
May 04 '11 at 21:25
source share

To create a view, you create a model, and then in the initializer you run the SQL statement to create the view directly against the context with the first line of code, and then in the context you override OnModelCreating and run the second line of code to ignore the model.

 context.Database.ExecuteSqlCommand(Resources.<resourcename>); modelBuilder.Ignore<modeltype>(); 
+12
Jun 12 '12 at 23:33
source share



All Articles