Link to home
Start Free TrialLog in
Avatar of RBS
RBS

asked on

Seeding a join table in MVC

Hi:

I have a database in MVC 5 that I am trying to seed.  The db consists of courses - each of which is made up of one or more modules.

Seeding the courses and modules works fine but I am having problems seeding the join-table course-modules - since at the time of seeding, there are no ids for either.

Here's my code - any suggestions on how to fix it greatly appreciated.

public class DatabaseInitializer :  DropCreateDatabaseIfModelChanges<DataContext>
{
	public override void Seed(DataContext context)
	{	
		public class Course 
		{
			public int Id { get; set; }
			public string Name { get; set; }
		}

		public class Module
		{
			public int Id { get; set; }
			public string Name { get; set; }
		}

		var course = new Course
		{
			Name = "Social Studies"
		};
		context.Courses.Add(course);

		var module = new Module
		{
			Name = "History",
		};
		context.AddModule(module)

		var module = new Module
		{
			Name = "Geography",
		};
		context.AddModule(module)
		
		//Problem - module and courses don't yet have Ids
		var courseModule = new CourseModule { CourseId = 1, ModuleId = 1 };
		context.CourseModules.Add(courseModule);
		courseModule = new CourseModule { CourseId = 1, ModuleId = 2};
		context.CourseModules.Add(courseModule);
		

		base.Seed(context)
	}
}

Open in new window

Avatar of ambience
ambience
Flag of Pakistan image

Relying on Code first conventions, You could add virtual properties to the domain objects like

            public class Course
            {
                  public int Id { get; set; }
                  public string Name { get; set; }
                        public virtual ICollection<Module> Modules { get;set;}
            }

            public class Module
            {
                  public int Id { get; set; }
                  public string Name { get; set; }
                        public virtual ICollection<Course> Courses { get;set;}
            }

and then configure the model as

        protected override void OnModelCreating(DbModelBuilder b)
        {
            base.OnModelCreating(b);
            b.Entity<Course>().HasMany(p => p.Modules).WithMany(p => p.Courses);

This would allow you to seed like

var course = new Course(...)
var module = new Module(...)
course.Modules.Add(module);
module.Courses.Add(course);
Avatar of RBS
RBS

ASKER

Hi Ambience:

Thanks for the reply but it does not answer the question.  The problem is not with seeding the main tables but with seeding the join tables.  I have since found a good answer at http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application

It looks something like this:

foreach (Enrollment e in enrollments)
            {
                var enrollmentInDataBase = context.Enrollments.Where(
                    s =>
                         s.Student.ID == e.StudentID &&
                         s.Course.CourseID == e.CourseID).SingleOrDefault();
                if (enrollmentInDataBase == null)
                {
                    context.Enrollments.Add(e);
                }
            }
            context.SaveChanges();

Open in new window


Regards,
RBS
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RBS

ASKER

Thanks ambience!

RBS