使用基於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