2016-03-01 49 views
1

在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屬性。如果我需要添加一些映射/配置也是一個選項。

回答

1

I have seen other SO threads that state to change the abstract property to virtual, but this is not an option as all derived classes MUST include the Amount property. If I need to add some mappings/configurations that is an option as well.

如果你讓它在基類的虛,那麼所有派生屬性包括金額屬性。事實上,作爲一種簡單的值類型屬性,它甚至不需要是虛擬的。

我看不出有理由強制派生類來覆蓋該屬性。如果您有其他具體原因希望該財產被重寫,請將其編輯爲您的問題。

+0

你是對的,這是我的錯誤設計。謝謝。 –

相關問題