Seeding Many to Many EF Code First Relationship

There are a few other posts on this topic that I saw but I was not able to get a correct answer yet (my own fault I am sure) but I want to seed a database and I have set up a many to many relationship, but I can't figure out how to seed the second entity with the first entities id.

        var users = new List<User>()
        {
            new User()
            {
                Id = 1,
                FirstName = "Clark",
                LastName = "Kent"
            },
            new User()
            {
                Id = 2,
                FirstName = "Lex",
                LastName = "Luther"
            }
        };
        users.ForEach(p => context.Users.Add(p));
        var messages = new List<Message>()
        {
            new Message()
            {
                Id = 1,
                SenderId = 2,
                Recipients = new List<User> { Id = 2, Id = 3}  // <<< Problem is here
            }
        }
        messages.ForEach(p => context.Messages.Add(p));            
        base.Seed(context);

My message class.

public class Message
{
    public int Id { get; set; }
    public int SenderId { get; set; }
    public int RecipientsId { get; set; }
    public virtual User Sender { get; set; }
    public virtual ICollection<User> Recipients { get; set; }
}

My user class.

public class User
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public virtual ICollection<Message> Messages { get; set; }
}

To be clear - I can properly seed users with no problem, if there are any typos or copy paste errors they aren't a big deal because I know everything is working besides being able to create a list of recipients that have already been seeded.

Thanks in advance

Answers


This is an old thread but Ive just had a similar problem so thought I would offer an answer. You need to query your users object created above

var messages = new List<Message>()
    {
        new Message()
        {
            Id = 1,
            SenderId = 2,
            Recipients = new List<User>()
            {
               users.Single(u => u.Id == 1),
               users.Single(u => u.Id == 2)
        }
    }

First thing i notice is that you are adding Ints to the recipients list, not user objects

What happens if you try code like this:

var users = new List<User>()
    {
        new User()
        {
            Id = 1,
            FirstName = "Clark",
            LastName = "Kent"
        },
        new User()
        {
            Id = 2,
            FirstName = "Lex",
            LastName = "Luther"
        }
    };
    users.ForEach(p => context.Users.Add(p));

    var messages = new List<Message>()
    {
        new Message()
        {
            Id = 1,
            SenderId = 2,
            Recipients = new List<User> {users[0],users[1] }  // <<< Problem is here
        }
    }

    messages.ForEach(p => context.Messages.Add(p));         

    context.SaveChanges();

or you could event try this:

var users = new List<User>()
    {
        new User()
        {
            Id = 1,
            FirstName = "Clark",
            LastName = "Kent"
        },
        new User()
        {
            Id = 2,
            FirstName = "Lex",
            LastName = "Luther"
        }
    };
    users.ForEach(p => context.Users.Add(p));

    var messages = new List<Message>()
    {
        new Message()
        {
            Id = 1,
            SenderId = 2,
            Recipients = new List<User> {context.Users.Where(u=>u.Id==1),context.Users.Where(u=>u.Id==1) }
        }
    }

    messages.ForEach(p => context.Messages.Add(p));         

    context.SaveChanges();

Need Your Help

Solr - use 64-bit Java, not 32-bit Java on Windows 7 64-bit

java windows-7 solr 32bit-64bit

I have a Windows 7 64-bit machine. I have Java Runtime Environment for both 32-bit and 64 bit installed on the machine. How do I tell Solr to use the 64-bit version of JRE when I start up Solr?

Best Approach for Configuration File for an Angular/RequireJS Application?

javascript configuration angularjs

Is there a commonly accepted best practice for maintaining a configuration file that is available on the client side (sort of an equivalent of the AppSettings section on the server side in an ASP.NET