Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a SQL view from EF 6.1 code first

I just changed my application from Database first to code first! What a great improvement in deploying!. But now i have the following problem. I generated my Code-first model from my database, but after recreating the database from the generated code, my views from my database are generated like tables!

How do I generate my views from code first? and/or map them to my entities if I need to generate them manually?

EDIT.

Luke McGregor's post certainly brought me close. Yes it generates the views now. But the migrations don't work.

When trying to do a Update-Database statement the initial output is that there still are code changes.

I therefore executed the Add-Migration xxx Command and fired the Update-Database command again.

EDIT 2:

Resolving a few differences between my Code-first code and the view's SQL code solved this issue!

like image 928
Spons Avatar asked Apr 09 '14 13:04

Spons


People also ask

How do I create a view using EF code First poco?

You cannot create a view from EF Code First ,you should add the script in the 'seed' script to pre populate the view .


1 Answers

You will need to create a manual migration with some raw SQL in it eg something along the lines of the below

public partial class MyMigration: DbMigration 
{ 
    public override void Up() 
    { 
        Sql("CREATE VIEW......"); 
    } 
}
like image 82
Not loved Avatar answered Sep 21 '22 14:09

Not loved