在EF 6.0中嘗試數據庫遷移時出現上述錯誤。我很確定這是由於該屬性在基類中聲明爲抽象,然後在派生類中被重寫。下面是一些僞代碼來說明這一點:實體框架6代碼優先錯誤:屬性'Foo'不是'Bar'類型的聲明屬性
public abstract class Base
{
[Required]
public abstract double Amount { get; set; }
}
public abstract class Foo : Base
{
// just showing that this class is in the inheritance chain, nothing is done with the Amount field
}
public class Bar : Foo
{
[Required]
public override double Amount { get; set; }
}
確切的錯誤是:
屬性「金額」是不是在類型「欄」聲明的屬性。通過使用Ignore方法或NotMappedAttribute數據註釋驗證是否未明確從模型中排除該屬性。確保它是一個有效的基本屬性。
我已經看到其他SO線程聲明將抽象屬性更改爲虛擬,但這不是一個選項,因爲所有派生類必須包含Amount屬性。如果我需要添加一些映射/配置也是一個選項。
你是對的,這是我的錯誤設計。謝謝。 –