0

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

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

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

4

1 回答 1

1

将您的属性标记为NotMapped并使用默认值:

public string fruitType = "Apple";
[NotMapped]
public string FruitType
{
    get { return fruitType; }
    set { fruitType = value ?? fruitType; }
}
于 2020-09-20T03:01:02.213 回答