Seeding many to many tables with Entity Framework
- by Doozer1979
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.