如何在没有列的情况下在模型中设置默认值?

问题描述

我正在尝试为模型中的属性设置认值

public string fruitType = "Apple";
public string FruitType
{
    get { return fruitType; }
    set { fruitType = value; }
}

但是,我遇到的问题是FruitType不是数据库表中的列,因此它引发了错误。即使该列不存在,我是否可以设置它?

解决方法

将您的媒体资源标记为NotMapped,并使用默认值:

public string fruitType = "Apple";
[NotMapped]
public string FruitType
{
    get { return fruitType; }
    set { fruitType = value ?? fruitType; }
}