[英]C# EF code-first create SQL view query
I was trying to create a view from EF code-first. 我试图先从EF代码创建视图。 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. 我可以先使用EF数据库来获取我的SQL视图查询,但不能先使用代码来获取它。
Can someone help me ? 有人能帮我吗 ?
Thanks... 谢谢...
You can create views inside a Migration using the Sql method: 您可以使用Sql方法在Migration内部创建视图:
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: 在dbContext中将实体映射到视图没有任何问题:
public class MyDbContext : DbContext
{
public DbQuery<MyModelView> MyView
{
get
{
// Don't track changes to query results
return Set<MyModelView>().AsNoTracking();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.