【问题标题】:Add extra column to entity model which is not present in database向数据库中不存在的实体模型添加额外的列
【发布时间】:2017-11-09 13:13:23
【问题描述】:

我有以下实体

[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()
    {}


}

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

'字段列表中的未知列总数'

正如预期的那样。

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

我正在使用 EF6。

【问题讨论】:

    标签: c# entity-framework ef-database-first


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-19
        • 2018-05-07
        • 1970-01-01
        • 2020-10-04
        • 1970-01-01
        • 2021-12-18
        • 2022-11-27
        • 1970-01-01
        相关资源
        最近更新 更多