How to generate many to many relationship data with Bogus in .Net core

70 Views Asked by At

Hi guys I have a problem about Bogus. Firstly please check my sample below.

public class Student
{
    public Guid Id { get; set; } = Guid.NewGuid();
    public string Name { get; set; }
    public int Age { get; set; }
    public List<StudentLaptop> StudentLaptops { get; set; }
}
public class Laptop
{
    public Guid Id { get; set; } = Guid.NewGuid();
    public string Brand { get; set; }
    public string Name { get; set; }
    public List<StudentLaptop> StudentLaptops { get; set; }
}
public class StudentLaptop
{
    public Guid StudentId { get; set; }
    public Guid LaptopId { get; set; }
    public Laptop Laptop { get; set; }
    public Student Student { get; set; }
}

public class AppDbContext: DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("constring");
        }
    }
    public DbSet<Student> Students { get; set; }
    public DbSet<Laptop> Laptops { get; set; }
    public DbSet<StudentLaptop> StudentLaptops { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<StudentLaptop>().HasOne(a => a.Student).WithMany(a => a.StudentLaptops);
        modelBuilder.Entity<StudentLaptop>().HasOne(a => a.Laptop).WithMany(a => a.StudentLaptops);
        modelBuilder.Entity<StudentLaptop>().HasKey(a=> new {a.StudentId, a.LaptopId});
        Seed(modelBuilder);
        base.OnModelCreating(modelBuilder);
    }
    private void Seed(ModelBuilder modelBuilder)
    {
        DataGenerator.Initialize(modelBuilder);
    }
}
 public class DataGenerator
 {

     public static void Initialize(ModelBuilder modelBuilder)
     {

         var studentFaker = new StudentFaker();
         var laptopFaker = new LaptopFaker();

         var fakeStudent = studentFaker.Generate(5);
         var fakeLaptop = laptopFaker.Generate(3);
         var studentLaptopFaker = new StudentLaptopFaker(fakeStudent,fakeLaptop);
         var fakeStudentLaptop = studentLaptopFaker.Generate(15);


         modelBuilder.Entity<Student>().HasData(fakeStudent);
         modelBuilder.Entity<Laptop>().HasData(fakeLaptop);
         modelBuilder.Entity<StudentLaptop>().HasData(fakeStudentLaptop);

     }
     public class StudentFaker : Faker<Student>
     {
         public StudentFaker()
         {
             RuleFor(a => a.Id, f => f.Random.Guid());
             RuleFor(g => g.Name, f => f.Person.FullName);
             RuleFor(g => g.Age, f => f.Random.Number(100));
         }
     }

     public class LaptopFaker : Faker<Laptop>
     {
         public LaptopFaker()
         {
             RuleFor(a => a.Id, f => f.Random.Guid());
             RuleFor(l => l.Brand, f => f.Commerce.Product());
             RuleFor(l => l.Name, f => f.Commerce.ProductName());
         }
     }
     public class StudentLaptopFaker : Faker<StudentLaptop>
     {
         public StudentLaptopFaker(List<Student>students, List<Laptop> laptops)
         {
             RuleFor(sl => sl.StudentId, f => f.PickRandom(students).Id);
             RuleFor(sl => sl.LaptopId, f => f.PickRandom(laptops).Id);
         }
     }
 }

As you can see above I have declared some rules. But I have a migration error like this;

'The seed entity for entity type 'StudentLaptop' cannot be added because another seed entity with the same key value for {'StudentId', 'LaptopId'} has already been added. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.'.

If you guys help me I will be very appreciate it.

0

There are 0 best solutions below