I used bogus to seed data in Entity Framework Core, but I get a foreign key constraint error:
SqliteException: SQLite Error 19: 'UNIQUE constraint failed: Vaccines.Id'.
This is my data generation class:
public static class DataGenerator
{
public static List<DailyImmunization> GenerateDailyImmunizations(int count)
{
using var context = new IvbContext();
context.Vaccines.LoadAsync();
var vaccines = context.Vaccines.ToList();
// Use the vaccineIds list as needed
var dosageValues = new List<short> { 1, 2, 3 }; // Replace with actual dosage values
var dailyImmunizations = new Faker<DailyImmunization>()
.RuleFor(di => di.Id, f => f.Random.Guid())
.RuleFor(di => di.Date, f => f.Date.PastDateOnly(2))
.RuleFor(di => di.Vaccine, f => f.PickRandom(vaccines))
.RuleFor(di => di.Dosage, f => f.PickRandom(dosageValues))
.Generate(count);
return dailyImmunizations;
}
}
And this is my class:
public class DailyImmunization
{
public virtual Guid Id { get; set; }
public virtual DateOnly Date { get; set; }
public virtual Guid VaccineId { get; set; }
public virtual short Dosage { get; set; }
public virtual required Vaccine Vaccine { get; set; }
}
This is the method that I called to save data:
private void GenerateDailyImmunization()
{
var dailyImmunizations = DataGenerator.GenerateDailyImmunizations(50);
_context.DailyImmunizations.AddRange(dailyImmunizations);
_context.SaveChanges();
}
If I change the RuleFor(di => di.Vaccine)
to RuleFor(di => di.VaccineId)
, I still get that foreign key constraint error.
I think I have to insert both fields but they are given randomly how do I add the VaccineId
of the same selected Vaccine
.
Edit:
The issue in the mapping of tables if I delete them the seeding will work. But I can not see what is the issue in my mapping code. and I need the to restrict delete:-
private void RelationshipsMapping(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Vaccine>().HasOne(x => x.VaccineType).WithMany(op => op.Vaccines)
.OnDelete(DeleteBehavior.Restrict).HasForeignKey(@"VaccineTypeId").IsRequired();
modelBuilder.Entity<Vaccine>().HasOne(x => x.TargetDisease).WithMany(op => op.Vaccines)
.OnDelete(DeleteBehavior.Restrict).HasForeignKey(@"TargetDiseaseId").IsRequired();
modelBuilder.Entity<Vaccine>().HasOne(x => x.RouteOfAdministration).WithMany(op => op.Vaccines)
.OnDelete(DeleteBehavior.Restrict).HasForeignKey(@"RouteOfAdministrationId").IsRequired();
modelBuilder.Entity<Vaccine>().HasMany(x => x.DailyImmunizations).WithOne(op => op.Vaccine)
.OnDelete(DeleteBehavior.Restrict).HasForeignKey(@"vaccineId").IsRequired();
modelBuilder.Entity<VaccineType>().HasMany(x => x.Vaccines).WithOne(op => op.VaccineType)
.OnDelete(DeleteBehavior.Restrict).HasForeignKey(@"VaccineTypeId").IsRequired();
modelBuilder.Entity<TargetDisease>().HasMany(x => x.Vaccines).WithOne(op => op.TargetDisease)
.OnDelete(DeleteBehavior.Restrict).HasForeignKey(@"TargetDiseaseId").IsRequired();
modelBuilder.Entity<RouteOfAdministration>().HasMany(x => x.Vaccines).WithOne(op => op.RouteOfAdministration)
.OnDelete(DeleteBehavior.Restrict).HasForeignKey(@"RouteOfAdministrationId").IsRequired();
modelBuilder.Entity<DailyImmunization>().HasOne(x => x.Vaccine).WithMany(op => op.DailyImmunizations)
.OnDelete(DeleteBehavior.Restrict).HasForeignKey(@"vaccineId").IsRequired();
}