I am trying to teach my self MVC3 and EF4 using code first and the DbContext generator, so forgive me if this is a silly question.
Basically I have a user class, and an email class; this is because i want each user to be able to have multiple email addresses. the classes are set up like this:
public class User
{
[Key]
public int Id { get; set; }
public string User_Name { get; set; }
public string Password { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public virtual ICollection<Email> Emails { get; set; }
}
public class Email
{
[Key]
public int Id { get; set; }
public string Address { get; set; }
public User User { get; set; }
}
I am happily manipulating the user class using the CRUD methods build by MVC3 and inserting users programmatically to “seed” the db with test data, the latter i am doing like so by overriding the Seed method in the DropCreateDatabaseAlways class like so:
public class dbInitializer : DropCreateDatabaseAlways<UserContext>
{
protected override void Seed(UserContext context)
{
var Users = new List<User>
{
new User { User_Name = "uname",
Password = "pword",
First_Name = "fname",
Last_Name = "sname",
}
};
Users.ForEach(u => context.Users.Add(u));
}
}
Now i would also like to add so email addresses, and because of the way i set up my classes code first obviously realises that each user can have multiple email addresses and each email addresses can belong only to one user because when creating a new user or email object the intellisense (VS10) presents me with Emails and User properties that are not actually part of either class.
My question is this: How do i add an email address to a user as its created, and how do i add an email address to a user that has been created previously?
Add emails to user as it is created:
To add an email to a previously created user, you first need a reference to the user:
Reply to comments:
new[]is shorthand fornew Email[](new Email array).Technically Eranga’s answer is a little more flexible. In mine, since arrays are fixed length, you can’t add an
Emailto theEmailscollection after it has been initialized as an array. You would need to either useList<Email>as in Eranga’s answer, or convert to a list like so before invoking.Add():I prefer arrays when possible because they are easier to type, less verbose in the code, and I generally add everything in the object initializer, so I don’t need to add it again later.
That said, after you save the
Userand get it back out of the db, yourEmailscollection property will not be a fixed-length array, and can be added to without having to invoke.ToList().