Complex Types of Nullable Values

c# entity-framework

Question

For a complex type in entity framework with only nullable properties, why is that for something like the following requires the complex type be instantiated:

[ComplexType]
public class Address {
    public string Address1 { get; set; }
}

public class Customer {
    [Key]
    public int CustomerId {get;set;}
    public Address Address {get;set;}
}

More specifically, if you don't declare and address type, you get a "Null value for non-nullable member. Member: 'Address'." As per this question.

As long as all the properties in the complex type are nullable, why is it that entity framework requires an instance of Address? Since Address1 is nullable, why can it not just assume that and create the table (and column Address_Address1) and null value in the row as if I had created an instance of Address with a null Address1?

Or is there an attribute/fluent setting I can apply to achieve that?

1
20
5/23/2017 10:30:09 AM

Popular Answer

Even if the properties are nullable, the class containing them isn't. You can have Address.Address1 as null, but Address itself has to be instantiated.

8
7/5/2013 11:37:57 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow