-1

As far as I can tell, this is a pretty classic error. I've tried to understand exactly why it happens, but I don't get why it happens in my code .. I have two classes, user:

public class User
    {
        [Key]
        public string Username { get; set; }
        public string DisplayName { get; set; }
        public List<Organization> Organizations { get; set; }
    }

and organization:

public class Organization
    {
        public int OrganizationId { get; set; }
        public string OrganizationName { get; set; }
    }

In my main program, my goal is to create a new organization with a name provided by the user. Then, I want to get a username from the user. Then, the organization first inputted should be added to this user. As far as I can tell, my main() is working on an instance of an object? Main looks like this: Console.Write("Enter name of organization: "); var orgName = Console.ReadLine();

            var org = new Organization{OrganizationName = orgName};
            db.Organizations.Add(org);
            db.SaveChanges();

            Console.WriteLine("Enter username: ");
            var userName = Console.ReadLine();
            var user = new User {Username = userName};
            user.Organizations.Add(org); //I get the exception on this line
            db.Users.Add(user);
            db.SaveChanges();

The exact exception is: "CodeFirstNewDatabaseSample.User.Organizations.get returned null."

1 Answers1

3

You need to initialize the Organizations property to an empty list:

public class User
{
    [Key]
    public string Username { get; set; }
    public string DisplayName { get; set; }
    public List<Organization> Organizations { get; set; } = new List<Organization>();
}

The following is not recommended as it is prone to produce exactly the problem you are having:
or:

var user = new User { Username = userName };
user.Organizations = new List<Organization>();
user.Organizations.Add(org);

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
  • 2
    Also, your edit made your answer worse. It's *recommended* to initialize all collections of EF entities in the constructor in order to avoid manually instantiating them (as your second example shows) – Camilo Terevinto Feb 09 '18 at 11:21
  • The reason for the downvote is my second comment. You shouldn't recommend not following best practices. There's a reason the EF scaffolder automatically generates the constructor with the instantiation of `HashSet` for collections – Camilo Terevinto Feb 09 '18 at 11:23
  • @CamiloTerevinto Ok, did not know that it was officially recommended. I write my models on my own and sometimes forget to initialize lists, but normally do it exactly that way. I updated the answer to reflect that. Thanks for your input! – Christoph Fink Feb 09 '18 at 11:28
  • You are welcome. As an FYI, the recommended version is `public ICollection Name { get; set; } = new HashSet();` – Camilo Terevinto Feb 09 '18 at 11:30
  • @CamiloTerevinto Do you by any chance have some reference for that? I'm really interested in the background for that exact recommendation, because I mostly use `List` in my models as they are easiest to work with in many of my use-cases and I like to avoid calling `ToList` to often... – Christoph Fink Feb 09 '18 at 11:34
  • 1
    @ChrFin I don't have an official reference at hand, but this kinds of summarizes it: https://stackoverflow.com/questions/38954108/what-is-a-proper-way-of-writing-entity-poco-classes-in-entity-framework-core/38954731 – Camilo Terevinto Feb 09 '18 at 11:56