[英]EF5 -> EF6 strange migration behavior
We have an ASP.Net MVC4 project that has been using Entity Framework 5 for a year now. 我们有一个ASP.Net MVC4项目,该项目已经使用Entity Framework 5一年了。 We just updated to Entity Framework 6 using the NuGet Package Manager and now we are getting odd behavior in the migration generation.
我们刚刚使用NuGet包管理器更新到Entity Framework 6,现在在迁移生成中出现了奇怪的行为。
A Many-to-many table that existed correctly, now EF wants to add an additional column and FK... and Columns that have the [NotMapped]
attribute are NOT being ignored. 正确存在的多对多表,现在EF想添加一个附加列和FK ...,并且不忽略具有
[NotMapped]
属性的列。 I had to use .Ignore()
我不得不使用
.Ignore()
Here is the relevent code for the Many-to-many that has been working with EF5, but suddenly doesn't want to work with EF6... 这是已经使用EF5的多对多的相关代码,但是突然之间不想使用EF6 ...
public class Grade
{
[Key]
public int GradeKey { get; set; }
[Required]
public string GradeLevel { get; set; }
public virtual ICollection<Test> Tests { get; set; }
}
public class Test
{
[Key]
[HiddenInput(DisplayValue = false)]
public int TestKey { get; set; }
...
[UIHint("_GradeCheckboxes")]
[Order(5)]
[Display(Order = 5)]
[DisplayName("Grades")]
public virtual ICollection<Grade> Grades { get; set; }
}
//Fluent API for the join
modelBuilder.Entity<Grade>().HasKey(p => p.GradeKey).HasMany(p => p.Tests).WithMany(d => d.Grades).Map(x =>
{
x.MapLeftKey("Grade_GradeKey");
x.MapRightKey("Test_TestKey");
x.ToTable("GradeTests");
});
// Migration
public override void Up()
{
AddColumn("dbo.Grades", "Test_TestKey", c => c.Int()); // This is a problem
AlterColumn("dbo.Students", "StateCode", c => c.String()); // This already exists in DB
AlterColumn("dbo.Students", "SchoolName", c => c.String()); // This already exists in DB
CreateIndex("dbo.Grades", "Test_TestKey"); // Problem
CreateIndex("dbo.StudentTests", "StudentKey");
CreateIndex("dbo.StudentTests", "TestKey");
AddForeignKey("dbo.Grades", "Test_TestKey", "dbo.Tests", "TestKey"); // Problem
AddForeignKey("dbo.StudentTests", "StudentKey", "dbo.Students", "StudentKey", cascadeDelete: true); // Already exists
AddForeignKey("dbo.StudentTests", "TestKey", "dbo.Tests", "TestKey", cascadeDelete: true); // Already exists
}
Has anyone else experience this problem when upgrading or with EF6 in general. 升级或使用EF6时,是否有其他人遇到此问题。 I have tried many different tactics and internet searches to try and solve this issue for a few days now.
我尝试了许多不同的策略和互联网搜索来尝试解决这一问题已有几天了。 I have even removed the
__MigrationHistory
table and deleted all of the migrations and has it start fresh from the models alone, but it still wants to create the Test_TestKey
column in the Grades
table instead of in the Many-to-many table... 我什至删除了
__MigrationHistory
表并删除了所有迁移,并使其仅从模型重新开始,但是它仍想在Grades
表中而不是在Many-to-many表中创建Test_TestKey
列。
I have dropped the [Key] attribute from Grade and tested and works as expected when intialising a basic database 我从Grade删除了[Key]属性,并进行了测试,并在初始化基本数据库时按预期工作
public class Grade
{
public int GradeKey { get; set; }
[Required]
public string GradeLevel { get; set; }
public virtual ICollection<Test> Tests { get; set; }
}
fluent api is the same asyours when building many to many table 建立多对多表格时,流利的api与您一样
modelBuilder.Entity<Grade>()
.HasKey(g => g.GradeKey)
.HasMany(g => g.Tests)
.WithMany(t => t.Grades)
.Map(map =>
{
map.ToTable("GradeTests");
map.MapLeftKey("GradeKey");
map.MapRightKey("TestKey");
});
Database tables 数据库表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.