2013-02-19 136 views
0

我有一個模型被用來填充數據庫在不更改數據庫的情況下更改模型?

public class Account 
{ 
    public int NumberOfPayPeriods { get { return 24; } } 
    public decimal YearAmount { get; set; } 
    public decimal PlanTotal 
    { 
     get { return NumberOfPayPeriods*YearAmount; } 
    } 
} 

NumberOfPayPeriods屬性,我需要從僅僅是一種get更改爲get; set;

但是,當我改變這一點,我得到一個EntityCommandExecutionException (無效的列名稱)。我認爲這是因爲它試圖將其映射到先前沒有這樣的列的數據庫(因爲它只是一個get)。

有什麼辦法可以改變這一個get; set;而不必刪除表格?那裏有很多重要的數據不會丟失或重新創建。

+0

見http://stackoverflow.com/questions/7237822/how-do-i-add-a-column-to-a-table-schema-upgrade-and-have-it-map-to -an-EF-代碼 – ChrisF 2013-02-19 22:38:55

回答

3

在您不想存儲的屬性上添加一個[NotMapped]屬性。

public class Account 
{ 
    [NotMapped] 
    public int NumberOfPayPeriods { get { return 24; } set { ... } } 
    public decimal YearAmount { get; set; } 
    public decimal PlanTotal 
    { 
     get { return NumberOfPayPeriods*YearAmount; } 
    } 
} 
相關問題