Required Properties on optional Entity Framework Complex Types

后端 未结 1 1233
再見小時候
再見小時候 2021-01-17 17:40

I want to define [Required] attributes on a Complex Type in Entity Framework. For example, I have a Customer entity with an optional Address

相关标签:
1条回答
  • 2021-01-17 17:51

    It's not possible with a complex type. You'll need to create an Address entity if you want it to be nullable.

    What EF will do with a complex type is map the properties to the same table - which it sounds like you've intended.

    Because of that - your schema for your example would look like this:

    enter image description here

    With a non-nullable column for Address_PostCode, since it's not valid in the database there's not a way for EF to create the row, without your object having an address, and a postcode.

    0 讨论(0)
提交回复
热议问题