简体   繁体   中英

MCV3 Entity Framework 4 Code First: Adding Sub Objects to the db

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:

protected override void Seed(UserContext context)
{
    var Users = new List<User>
    {
        new User {  User_Name = "uname",
                    Password = "pword",
                    First_Name = "fname",
                    Last_Name = "sname",
                    Emails = new[]
                    {
                        new Email { Address = "email1@domain.tld" },
                        new Email { Address = "email2@domain.tld" },
                    }
        }
    };

    Users.ForEach(u => context.Users.Add(u));
}

To add an email to a previously created user, you first need a reference to the user:

var user = Users.First();
user.Emails.Add(new Email { Address = "email3@domain.tld" });

Reply to comments:

new[] is shorthand for new 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 Email to the Emails collection after it has been initialized as an array. You would need to either use List<Email> as in Eranga's answer, or convert to a list like so before invoking .Add() :

user.Emails.ToList().Add(new Email { Address = "email3@domain.tld" });

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 User and get it back out of the db, your Emails collection property will not be a fixed-length array, and can be added to without having to invoke .ToList() .

Adding Email to a new user

protected override void Seed(UserContext context)
{
    var Users = new List<User>
    {
        new User {  User_Name = "uname",
                    Password = "pword",
                    First_Name = "fname",
                    Last_Name = "sname",
                    Emails = new List<Email> { new Email { Addess = "foo@bar.baz" } }
        }
    };

    Users.ForEach(u => context.Users.Add(u));
}

Similarly for existing user.

user.Emails.Add(new Email { Addess = "foo@bar.baz" });

user.Emails.Add will issue a database request to load the Email collection (ie. Lazy Loading). Alternate way to do this is

var email = new Email { Addess = "foo@bar.baz", User = user };
context.Emails.Add(email);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM