【问题标题】:Changing a model without altering the database?更改模型而不更改数据库?
【发布时间】:2013-02-20 06:38:52
【问题描述】:

我有一个用于填充数据库的模型

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;无需删除表?那里有很多不能丢失或重新创建的重要数据。

【问题讨论】:

标签: c# entity-framework


【解决方案1】:

在您不想存储的属性上添加[NotMapped] 属性。

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

【讨论】:

    猜你喜欢
    • 2015-01-19
    • 2013-07-02
    • 1970-01-01
    • 2017-08-24
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多