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."