Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex Types of Nullable Values

Tags:

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?

like image 821
Gene Avatar asked May 05 '13 02:05

Gene


People also ask

What are nullable value types?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false . However, in some applications a variable value can be undefined or missing.

What are nullable types in C hash?

The Nullable type allows you to assign a null value to a variable. Nullable types introduced in C#2.0 can only work with Value Type, not with Reference Type. The nullable types for Reference Type is introduced later in C# 8.0 in 2019 so that we can explicitly define if a reference type can or can not hold a null value.

What is nullable and non nullable value type?

Nullable variables may either contain a valid value or they may not — in the latter case they are considered to be nil . Non-nullable variables must always contain a value and cannot be nil . In Oxygene (as in C# and Java), the default nullability of a variable is determined by its type.

How do you handle nullable values in C#?

Empty(A constant for empty strings). This method will take a parameter that will be of System. String type. The method will return a Boolean value, like n case if the argument String type has null or empty string (“”), then it will return True value else it will return False value.


1 Answers

look at this question Entity Framework 5 DbUpdateException: Null value for non-nullable member.

you need to instantiate the complex type even if all the properties are null.

like image 104
Parv Sharma Avatar answered Sep 21 '22 19:09

Parv Sharma