Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7674019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:38:47+00:00 2026-05-31T16:38:47+00:00

I am trying to teach my self MVC3 and EF4 using code first and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:38:48+00:00Added an answer on May 31, 2026 at 4:38 pm

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to self-teach myself how to code but this practice problem is
I'm basically trying to teach myself how to code and I want to follow
I am trying to teach my self F# by porting some Haskell Code. Specifily
I am trying to teach myself MySQL/PHP from the very beginning. The following code
I am a novice programmer who is trying to teach myself to code, specifically
Hey guys, I'm new to UNIX, trying to teach myself and came across this
I'm trying to self-teach myself C++, and once again I got stuck on something
Before we start, I apologize for my MYSQL novice status. I'm trying to self-teach
I am trying to teach myself jquery while reading someone else's code, and I
I'm trying to teach myself python using Google's AppEngine , and I can't get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.