如何首先使用EF代码映射现有的SQL Server视图 [英] How to map exisiting sql server view with EF code first
问题描述
我在EF方面还很陌生,并且首先学习EF代码。我正在寻找一种知识,以首先将现有的SQL Server视图与EF代码映射。我已经用POCO映射了我的视图,但是得到了以下错误。
i am fairly new in EF and learning EF code first. i am looking for a knowledge to map exisiting sql server view with EF code first. i have map my view with POCO but getting the below error.
当我尝试从视图中获取数据然后抛出了以下错误
when i try to fetch data from view then got the below error thrown
其他信息:自创建数据库以来,支持 TestDBContext上下文
的模型已更改。考虑使用代码优先
迁移来更新数据库
Additional information: The model backing the 'TestDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database
我的完整代码如下
my full code as follow
public class TestDBContext : DbContext
{
public TestDBContext()
: base("name=TestDBContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new vwCustomerConfiguration());
}
public DbSet<vwCustomer> vwCustomer { get; set; }
}
public class vwCustomerConfiguration : EntityTypeConfiguration<vwCustomer>
{
public vwCustomerConfiguration()
{
this.HasKey(t => t.CustomerID);
this.ToTable("vwCustomer");
}
}
public class vwCustomer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
}
这样,我试图加载数据。
this way i am trying to load data.
using (var db = new TestDBContext())
{
var listMyViews = db.vwCustomer.ToList();
}
指导我在抛出错误的代码中所缺少的内容。谢谢
guide me what i am missing in code for which error is throwing. thanks
当我发出Add-Migration My_vwCustomer时,我看到了如下所示的新迁移代码一。
When i issue Add-Migration "My_vwCustomer" then i saw new migration code added as below one. it seems there is no migration is pending.
public partial class My_vwCustomer : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.vwCustomers",
c => new
{
CustomerID = c.Int(nullable: false, identity: true),
FirstName = c.String(),
})
.PrimaryKey(t => t.CustomerID);
}
public override void Down()
{
DropTable("dbo.vwCustomers");
}
}
推荐答案
OP的反馈:
当我使用ADO.Net实体模型向导生成视图时,
一切正常可以。
When i generate the view with ADO.Net Entity model wizard then everything works fine.
您可以如下所示。
注意:我从 此帖子 。
- 为视图创建POCO类;例如
FooView
- 在<$ c $中添加
DbSet
属性c> DbContext 类 -
使用
FooViewConfiguration
文件为视图设置其他名称
(在构造函数中使用ToTable( Foo)
;)或设置特定的
属性
- Create a POCO class for the view; for example
FooView
- Add the
DbSet
property in theDbContext
class Use a
FooViewConfiguration
file to set a different name for the view (usingToTable("Foo")
; in the constructor) or to set particular properties
public class FooViewConfiguration : EntityTypeConfiguration<FooView>
{
public FooViewConfiguration()
{
this.HasKey(t => t.Id);
this.ToTable("myView");
}
}
将FooViewConfiguration文件添加到modelBuilder中,例如
通过Context的OnModelCreating方法:
Add the FooViewConfiguration file to the modelBuilder, for example ovveriding the OnModelCreating method of the Context:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new FooViewConfiguration ());
}
根据上述配置,现在您的表为
this.ToTable( myView);
。换句话说, myView
。
这是 EF查询
,用于检索所有数据
> myView 表。
Here is the EF query
to retrieve all the data
on the myView
table.
var listMyViews = yourDbContext.myView.ToList()
您的预测可能是这样的:
Your projection may be like this :
var query = yourDbContext.myView
.Select(v=> new
{
ID = v.ID,
EmpName = v.EmpName,
Salary = v.Salary
}).ToList();
这篇关于如何首先使用EF代码映射现有的SQL Server视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!