2013-02-19 126 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; } 
    } 
} 
相关问题