Entity framework EF代码首先告诉我对已经在db中的db对象进行迁移
我首先使用EF代码。所以最初我在数据库中并没有表。所以我写了一些类,当我查询这些类时,我看到EF代码首先在db中创建这些表,但当我在db中创建sql server视图,然后在c#&EF项目中用我的代码映射该视图时,当我尝试查询该视图时,我得到如下错误消息 其他信息:自创建数据库以来,支持“TestDBContext”上下文的模型已更改。考虑使用代码第一迁移来更新数据库>/P> 我知道EF告诉我进行迁移,但如果我迁移,EF将在db中的视图已经存在时再次在db中创建该视图 那么,请告诉我如何通知EF我的视图已经在db中,因此不需要迁移。 请引导我。谢谢 编辑1 第一次我的数据库没有表。所以我写了一些类似下面的课程Entity framework EF代码首先告诉我对已经在db中的db对象进行迁移,entity-framework,ef-code-first,Entity Framework,Ef Code First,我首先使用EF代码。所以最初我在数据库中并没有表。所以我写了一些类,当我查询这些类时,我看到EF代码首先在db中创建这些表,但当我在db中创建sql server视图,然后在c#&EF项目中用我的代码映射该视图时,当我尝试查询该视图时,我得到如下错误消息 其他信息:自创建数据库以来,支持“TestDBContext”上下文的模型已更改。考虑使用代码第一迁移来更新数据库>/P> 我知道EF告诉我进行迁移,但如果我迁移,EF将在db中的视图已经存在时再次在db中创建该视图 那么,请告诉我如何通知EF
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; }
}
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");
}
}
稍后,我在db中创建了一个视图,并在下面的代码中引用该视图
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; }
}
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");
}
}
错误是附加信息:自创建数据库以来,支持“TestDBContext”上下文的模型已更改。考虑使用代码第一迁移来更新数据库< /Code > < /P>
感谢正常添加新迁移,并从向上(和向下)方法中的迁移代码中删除尝试手动创建新表的代码(在向上和向下中调用CreateTable方法)。然后将迁移应用到数据库中,一切都会完美地工作
不幸的是,自动迁移生成不是一个非常智能的工具,通常需要手动指定数据库应该如何更改。在EF迁移文档中指出,手动编辑迁移代码是完全可以的。另一种方法,我们可以这样做,它解决了我的问题。请参阅代码
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<YourDbContext>(null);
base.OnModelCreating(modelBuilder);
}
我想这也会起作用,但不是我自己测试的
我们可以使用Fluent API使用Ignore方法对其进行配置:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Ignore<MyClass>();
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
Ignore();
}
您能为我们显示代码吗?@Sampath此处显示完整代码。@Sampath查看我的更新代码并查看我的答案。谢谢如果您想使用migrations系统更新数据库模式,那么您的方法将遇到问题。如果您仅手动或使用sql脚本更新数据库,则完全可以。@mr100“是”,您有权获得第一个答案或第一组代码。我再次更新了答案。
public partial class AddingvwCustomer : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Ignore<MyClass>();
}