0

I get this error when I 'update-database -Verbose' using Powershell:

The value of the complex property 'Address' on entity of type 'Student' is null. Complex properties cannot be set to null and values cannot be set for null complex properties.

or

{"Null value for non-nullable member. Member: 'Address'."}

This error also occurs when I try to create a new student or instructor. I think the problem is in the controller but that is 100's of line of code and I am not exactly sure where it is. Here is the code for Student.cs:

public class Student : Person
{
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    [Display(Name = "Enrollment Date")]
    public DateTime EnrollmentDate { get; set; }

    // Credits earned
    [Display(Name = "Credits Earned")]
    [Range(typeof(double), "0.00", "1000.00")]
    public double? CreditsEarned { get; set; }

    // GPA
    [Display(Name = "GPA")]
    [Range(typeof(double), "0.7", "4.0")]
    public double? Gpa { get; set; }

   public Address Address { get; set; }  // here is me using Address


    public virtual ICollection<Enrollment> Enrollments { get; set; }
}

Person class:

public abstract class Person
{
    public int ID { get; set; }

    // A Person 'has a' Address object 
    public Address Address { get; set; }

    [Required]
    [StringLength(50)]
    [Display(Name = "Last Name")]

    public string LastName { get; set; }
    [Required]
    [StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
    [Column("FirstName")]
    [Display(Name = "First Name")]
}

And here is the Address class I created:

public class Address
{
    [Required]
    [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]
    public string Email { get; set; }

    [Required]
    [Compare("Email")]
    public string EmailConfirm { get; set; }
}
4
  • 1
    What is "Address", where is it defined and how? Commented Mar 20, 2015 at 12:40
  • in the abstract class Person public Address Address {get; set; } and I created an Address class --> you can see in the edits Commented Mar 20, 2015 at 12:52
  • 1
    Have you tried setting it to virtual? Commented Mar 20, 2015 at 12:52
  • Yes and it doesn't work, thanks Commented Mar 20, 2015 at 12:59

2 Answers 2

1

Your Person class is never creating an instance of Address so it is null. Add a constructor that creates an address to your Person class

public Person() 
{
    Address = new Address();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Are you initializing your Address property when you create a Student instance?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.