【发布时间】:2016-09-13 08:54:36
【问题描述】:
我首先使用 EF 代码。所以最初我在数据库中没有表。所以我写了一些类,当查询这些类时,我看到 EF 代码首先在 db 中创建这些表,但是当我在 db 中创建 sql server 视图,然后用我在 c# 和 EF 项目中的代码映射该视图时,当我尝试查询时查看然后我收到如下错误消息。
附加信息:支持“TestDBContext”上下文的模型自数据库创建以来已更改。考虑使用 Code First 迁移来更新数据库
我知道 EF 告诉我进行迁移,但如果我迁移,那么当视图在 db 中已经存在时,EF 将再次在 db 中创建该视图。
所以告诉我如何通知 EF 我的视图已经在 db 中,因此不需要迁移。 请指导我。谢谢
编辑 1
我的数据库第一次没有表。所以我写了一些像下面这样的类。
public class CustomerBase
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
}
public class Customer : CustomerBase
{
public virtual List<Addresses> Addresses { get; set; }
}
public class Addresses
{
[Key]
public int AddressID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public bool IsDefault { get; set; }
public virtual List<Contacts> Contacts { get; set; }
public int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
}
public class Contacts
{
[Key]
public int ContactID { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public bool IsDefault { get; set; }
public int AddressID { get; set; }
public virtual Addresses Customer { get; set; }
}
public class TestDBContext : DbContext
{
public TestDBContext()
: base("name=TestDBContext")
{
}
public DbSet<Customer> Customer { get; set; }
public DbSet<Addresses> Addresses { get; set; }
public DbSet<Contacts> Contacts { get; set; }
}
当我像下面的查询一样查询客户时,EF 在幕后的 db 中创建所有必需的表。
var bsCustomer = (from cu in db.Customer
where (cu.CustomerID == 2)
select new
{
cu,
Addresses = from ad in cu.Addresses
where (ad.IsDefault == true)
from ct in ad.Contacts
select ad,
}).ToList();
稍后我在 db 中创建一个视图,并在下面的代码中引用该视图。
public partial class vwCustomer
{
[Key]
public int CustomerID { get; set; }
public string FirstName { get; set; }
}
public class vwCustomerConfiguration : EntityTypeConfiguration<vwCustomer>
{
public vwCustomerConfiguration()
{
this.HasKey(t => t.CustomerID);
this.ToTable("vwCustomers");
}
}
所以现在我的 DbContext 如下所示,带有视图类参考
public class TestDBContext : DbContext
{
public TestDBContext()
: base("name=TestDBContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new vwCustomerConfiguration());
}
public DbSet<Customer> Customer { get; set; }
public DbSet<Addresses> Addresses { get; set; }
public DbSet<Contacts> Contacts { get; set; }
public virtual DbSet<vwCustomer> vwCustomers { get; set; }
}
在我尝试查询视图时发生错误
using (var db = new TestDBContext())
{
var listMyViews = db.vwCustomers.ToList();
}
错误是Additional information: The model backing the 'TestDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database
谢谢
【问题讨论】:
-
你能给我们看代码吗?
-
@Sampath 此处显示完整代码。
-
@Sampath 查看我更新的代码并查看我的答案。谢谢
标签: entity-framework ef-code-first