使用基于EF6的代码为什么Add-Migration突然生成非空代码迁移?
我的实际实体并未更改,但添加了新的DBSet以允许直接查询导航属性。
如果我运行添加迁移,它会生成一个非空迁移,只需对外键重新命名即可。
public override void Up()
{
RenameColumn(table: "dbo.ConfigurationPropertyBases", name: "ConfigurationClass_Id", newName: "ConfigurationClass_Id1");
RenameIndex(table: "dbo.ConfigurationPropertyBases", name: "IX_ConfigurationClass_Id", newName: "IX_ConfigurationClass_Id1");
}
它为什么会产生这样的迁移?
变化的DbContext:
public class ConfigurationContext : DbContext
{
//(...)
public DbSet<ConfigurationPackage> Packages { get; set; }
public DbSet<ConfigurationPropertyBase> ConfigurationPropertyBases { get; set; }
// THIS WAS ADDED
public DbSet<ConfigurationClass> ConfigurationClass { get; set; }
}
的DB模式:(我只显示相关的导航性能)。
public class ConfigurationPackage
{
public int Id { get; set; }
//(...)
public List<ConfigurationClass> Configurations { get; set; }
}
public class ConfigurationClass
{
public int Id { get; set; }
public List<ConfigurationPropertyBase> ConfigurationProperties { get; set; }
}
public abstract class ConfigurationPropertyBase
{
public int Id { get; set; }
public string Name { get; set; }
//(no navigation propeties here)
}
我有一些来自ConfigurationPropertyBase继承的类,但他们只包含简单的属性,如int或字符串(没有导航性能),但其中之一是:
public class ConfigurationPropertyComplex : ConfigurationPropertyBase
{
public ConfigurationClass ConfigurationClass { get; set; }
}
您还可以添加ConfigurationPropertyBase的代码吗?同样,考虑到名称中包含Base这个词,我会询问在您的模型中是否有派生类已经改变。 – strongbutgood
我确实发现了一个变化。我编辑了这个问题。它不在模型类中,而是在DbContext中。但问题仍然存在。为什么会产生这样的迁移? – Eiver
EF生成将关系存储在数据库中所需的列,然后在您使用POCO时在DbContext内部无声管理它们。因为您需要有一些方法来关联两个实体,所以EF将使用父级的PK来定义子实体上的隐式外键。我会更新我的答案以匹配。 – strongbutgood