向数据库中不存在的实体模型添加额外的列

Add extra column to entity model which is not present in database

我有以下实体

[Table("classes")]
public class Class
{

    public int classID { get; set; }
    public Nullable<int> grade { get; set; }
    public string classname { get; set; }
    public Nullable<bool> Inactive { get; set; }
    public Nullable<int> total { get; set; }

    public virtual ICollection<Student> Students { get; set; }

    public Class()
    {}


}

此实体有时由存储过程使用,有时由 entityframework(数据库优先方法)使用。最近我添加了一个存储过程使用的字段“total”。现在 entity framework 给出错误

'unknown column total in field list'

符合预期。

我的问题是,有没有一种方法可以在 onModelCreated 函数中为 total 列设置默认值,以便它忽略模型更改异常?

我正在使用 EF6。

您可以通过设置 DefaultValue 属性为 属性 设置默认值。

[DefaultValue(0)]
public Nullable<int> total { get; set; }

我认为您可以将 [NotMapped] 属性应用于总计,以告诉 EF 它不在数据库中