Code First CTP4:为什么参考属性忽略[Required]属性? [英] Code First CTP4: Why is the [Required] attribute ignored for reference properties?
问题描述
如果我将原始属性标记为[Required],则生成DB列不可为空。 但如果我将引用属性标记为[必需],则会被忽略: 生成的DB列仍然可以为空。 如果我想要它生成Not Nullable,
我必须通过上下文配置它 -  这是一种痛苦,并且不一致。
虽然这是记录在案的行为,但对我来说似乎很奇怪。您是否计划扩展[必需]以使用参考属性?
Richard
嗨理查德,
这是很好的反馈,这是我们没有真正考虑过支持的东西,我们将在未来版本中考虑添加它。
如果您在类中公开了一个外键属性,那么您可以添加[Required]属性,但我可以看到在实际引用上允许它将是
当你没有在课堂上公开外键时很有用。
〜罗文跨度> 跨度>
If I mark a primitive property as [Required] then the DB column is generated Not Nullable. But if I mark a reference property as [Required] it is ignored: the generated DB column is still nullable. If I wanted it generated Not Nullable, I have to configure this through the context - which is a pain, and inconsistent.
Although this is documented behaviour, it seems very odd to me. Do you have plans to extend [Required] to work with reference properties ?
Richard
Hi Richard,
This is good feedback, this is something we hadn’t really considered supporting, we will look at adding it in a future release.
If you expose a foreign key property in your class then you could add the [Required] attribute to that, but I can see that allowing it on the actual reference would be useful when you don’t have the foreign key exposed in your class.
~Rowan
这篇关于Code First CTP4:为什么参考属性忽略[Required]属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!