Seeding many to many tables with Entity Framework

Posted by Doozer1979 on Stack Overflow See other posts from Stack Overflow or by Doozer1979
Published on 2012-03-24T17:10:19Z Indexed on 2012/03/24 17:29 UTC
Read the original article Hit count: 239

I have a meeting entity and a users entity which have a many to many relationship.

I'm using Autopoco to create seed data for the Users and meetings How do i seed the UserMeetings linking table that is created by EntityFramework with seed data?

The linking table has two fields in it; User_Id, and Meeting_ID.

I'm looping through the list of users that autopoco creates and attaching a random number of meetings

Here's what i've got so far.

 foreach (var user in userList)
        {

            var rand = new Random();

            var amountOfMeetingsToAdd = rand.Next(1, 300);

            for (var i = 0; i <= amountOfMeetingsToAdd; i++)
            {
                var randomMeeting = rand.Next(1, MeetingRecords);

                //Error occurs on This line
                user.Meetings.Add(_meetings[randomMeeting]);
            }

        }

I got an 'Object reference not set to an instance of an object.' even though the meeting record that i'm trying to attach does exist.

For info all this is happening prior to me saving the context to the DB.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET