I was trying to create a view from EF code-first. Unfortunately after my research, I couldn't find any topics about it.
I can get my SQL view queries using EF database-first, but can't get it from code-first.
Can someone help me ?
Thanks...
I was trying to create a view from EF code-first. Unfortunately after my research, I couldn't find any topics about it.
I can get my SQL view queries using EF database-first, but can't get it from code-first.
Can someone help me ?
Thanks...
You can create views inside a Migration using the Sql method:
public partial class Migration : DbMigration
{
public override void Up()
{
this.Sql("CREATE VIEW dbo.MyView AS (etc)");
}
public override void Down()
{
this.Sql("DROP VIEW dbo.MyView");
}
}
There's not any problem to map an entity to a view, in your dbContext:
public class MyDbContext : DbContext
{
public DbQuery<MyModelView> MyView
{
get
{
// Don't track changes to query results
return Set<MyModelView>().AsNoTracking();
}
}
}