Home > Software engineering >  Seeding with ASP.NET Core with one to many relationships
Seeding with ASP.NET Core with one to many relationships

Time:02-04

I am trying to create a seed for my database in ASP.NET Core but I am having trouble with the relationships between the models. I have 3 models with 2 relationships. I have the following models:

public enum Grade
{
    A, B, C, D, F
}

public class Enrollment
{
    public Guid ID { get; set; } = Guid.NewGuid();

    public Course Course { get; set; }
    public Student Student { get; set; }
    public Grade Grade { get; set; }
}

public class Course
{
    public Guid ID { get; set; } = Guid.NewGuid();

    public string Title { get; set; }
    public int Credits { get; set; }

    public List<Enrollment>? Enrollments { get; set; }
 }

public class Student
{
   public Guid ID { get; set; } = Guid.NewGuid();

   public string LastName { get; set; }
   public string FirstName { get; set; }
   public DateTime EnrollmentDate { get; set; } = DateTime.Now;

   public List<Enrollment>? Enrollments { get; set; }
}

On my DBContext I try to create the seed:

List<Student> students = new List<Student>()
            {
                new Student {FirstName = "Jaimie", LastName = "Vos", EnrollmentDate = DateTime.Now },
                new Student {FirstName = "Bas", LastName = "Milius", EnrollmentDate = DateTime.Now },
                new Student {FirstName = "Rien", LastName = "Bijl", EnrollmentDate = DateTime.Now },
                new Student {FirstName = "Rajeck", LastName = "Massa", EnrollmentDate = DateTime.Now }
            };

    modelBuilder.Entity<Student>().HasData(students);

    List<Course> courses = new List<Course>()
            {
                new Course {Title = "Wiskunde", Credits = 20},
                new Course {Title = "Nederlands", Credits = 15},
                new Course {Title = "Frans", Credits = 10},
            };

    modelBuilder.Entity<Course>().HasData(courses);

    Enrollment test = new Enrollment();
    test.Grade = Grade.A;
    test.Course = courses[0];
    test.Student = students[1];

    modelBuilder.Entity<Enrollment>().HasData(test);

But when I run this I get the error:

The seed entity for entity type 'Enrollment' cannot be added because no value was provided for the required property 'CourseID'.

I followed the documentation for relations, does someone know a way to fix this issue?

CodePudding user response:

I've found that you can just create a new object (not specifying the type) and give it a property specifying the related ID. So you should be able to do something like the following:

modelBuilder.Entity<Course>().HasData(new [] {
  new { Title = "Frans", Credits = 10, Id = <courseGUID1> },
  new { Title = "Nederlands", Credits = 15, Id = <courseGUID2> }
});

modelBuilder.Entity<Enrollment>().HasData(new [] {
  new { Grade = Grade.A, CourseId = <courseGUID1>, StudentId = <studentGUID1> }
});

In conclusion, I hate seed data, but have used it a fair amount and the above solution seems to get me by. It's not intuitive and would be nice if the code you had worked!

I would assume that the IDs should be generated when created, but obviously that doesn't work.

PS: If using auto-incrementing integer IDs, I usually just use negative integers to avoid conflict with the generated IDs.

  •  Tags:  
  • Related