对于使用.Net Core 2.0在Visual Studio 2017中开发的项目,我有一个相当基本的模型 . 模型匹配以下内容
public class QuoteModel : BaseModel{
public QuoteModel()
{
GetQuoteItems = new List<QuoteItemModel>();
}
[Required]
public int QuoteTypeID { get;set; }
[Required]
[ForeignKey("QuoteTypeID")]
public QuoteTypeModel QuoteType { get;set;}
public ICollection<QuoteItemModel> GetQuoteItems { get; set; }
}
我已经尝试了有和没有[必需]注释,但仍然遇到这个问题 . 问题是,如果我创建一个新的Quote,QuoteTypeID默认为0,所以如果用户没有选择类型,模型仍然有效 . 我更喜欢如果模型通过不将外键默认为值而使自己无效,但是我无法弄清楚为什么外键默认为一个值,因为我的研究表明它通常默认为null,需要一个提供身份证 .
如何让外键'QuoteTypeID'没有默认值但需要设置值?
另外,为了清楚起见,BaseModel只是在我的大多数模型中提供了一些我想要的标准字段,如'CreatedOn'和'CreatedBy' .
1 回答
应该放在外键本身之上,并且应该在引号中有型号名称,即