The INSERT statement conflicted with the FOREIGN KEY constraint The statement has been terminated

asp.net-core-2.2 c# ef-code-first entity-framework-core sql-server

Question

I have a migration seed data that I wish to run when OnModelCreating method runs in my application.

Part of the data are for these two entities - State and LGA and so I have these two lines in addition to others in my

protected override void OnModelCreating(ModelBuilder modelBuilder)

method in ApplicationDbContext.

 ...
 public DbSet<State> States { get; set; }
 public DbSet<LGA> LGAs { get; set; }
 ...

and I have a method Seed() which is an extension method which I expect to run to seed the two tables.

I call the Seed() method as follows:

modelBuilder.Seed();

The extension method is defined like this:

public static class ModelBuilderExtensions
{
    public static void Seed(this ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<State>().HasData(
                new State { Id = 1, Name = "Abia" },
                new State { Id = 2, Name = "Adamawa" },
                new State { Id = 3, Name = "Akwa Ibom" },
                new State { Id = 4, Name = "Anambra" },
                new State { Id = 5, Name = "Bauchi" },
                new State { Id = 6, Name = "Bayelsa" },
                new State { Id = 7, Name = "Benue" },
                new State { Id = 8, Name = "Borno" },
                new State { Id = 9, Name = "Cross River" },
                new State { Id = 10, Name = "Delta" },
                new State { Id = 11, Name = "Ebonyi" },
                new State { Id = 12, Name = "Edo" },
                new State { Id = 13, Name = "Ekiti" },
                new State { Id = 14, Name = "Enugu" },
                new State { Id = 15, Name = "Gombe" },
                new State { Id = 16, Name = "Imo" },
                new State { Id = 17, Name = "Jigawa" },
                new State { Id = 18, Name = "Kaduna" },
                new State { Id = 19, Name = "Kano" },
                new State { Id = 20, Name = "Katsina" },
                new State { Id = 21, Name = "Kebbi" },
                new State { Id = 22, Name = "Kogi" },
                new State { Id = 23, Name = "Kwara" },
                new State { Id = 24, Name = "Lagos" },
                new State { Id = 25, Name = "Nasarawa" },
                new State { Id = 26, Name = "Niger" },
                new State { Id = 27, Name = "Ogun" },
                new State { Id = 28, Name = "Ondo" },
                new State { Id = 29, Name = "Osun" },
                new State { Id = 30, Name = "Oyo" },
                new State { Id = 31, Name = "Plateau" },
                new State { Id = 32, Name = "Rivers" },
                new State { Id = 33, Name = "Sokoto" },
                new State { Id = 34, Name = "Taraba" },
                new State { Id = 35, Name = "Yobe" },
                new State { Id = 36, Name = "Zamfara" },
                new State { Id = 37, Name = "Abuja (FCT)" },
                new State { Id = 38, Name = "Non Nigerian" }
                );

        modelBuilder.Entity<LGA>().HasData(
            new LGA { Id = 1, StateId = 1, Name = "Aba North" },
            new LGA { Id = 2, StateId = 1, Name = "Aba South" },
            new LGA { Id = 3, StateId = 1, Name = "Arochukwu" },
            ...
            new LGA { Id = 784, StateId = 16, Name = "Ahiazu - Mbaise" },
            new LGA { Id = 785, StateId = 38, Name = "Foreign" },
            new LGA { Id = 786, StateId = 18, Name = "Kaduna South" },
            new LGA { Id = 787, StateId = 16, Name = "Aboh - Mbaise" },
            new LGA { Id = 788, StateId = 9, Name = "Odukpani" }

This is the relationship between the two entities

public class LGA
{
    public int Id { get; set; }
    [Display(Name="State")]
    public int StateId { get; set; }
    [Required]
    public string Name { get; set; }

    public ICollection<Town> Towns { get; set; }
    [ForeignKey("StateId")]
    public State State { get; set; }

    public ICollection<ApplicationUser> Residents { get; set; }
}


public class State
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Display(Name="State Code")]
    public string Code { get; set; }

    public ICollection<LGA> LGAs { get; set; }
    public ICollection<ApplicationUser> Residents { get; set; }
}

But when I run update-database, I get the following error:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_LGAs_States_StateId". The conflict occurred in database...table "dbo.States", column 'Id'.

And I have made sure that all the states are included in the migration code.

However, I realize that when I check the States table generated, I cannot seem to find that last row in the table, even though it is there in the data seeding code

I am running ASP.NET Core 2.2 with Entity Framework Core on a Windows 10 O.S.

Where could I be going wrong please?

Thank you for your assistance.

1
0
10/5/2019 3:27:53 PM

Popular Answer

call seed function from startup.cs. Create Configuration class & inject it into Configure function of startup class. This is the another workaround for seeding data..`

public class Startup
{
    public void Configure(Configurations dbMigrationsConfig)
    {
        try
        {
            dbMigrationsConfig.SeedData().Wait();
        }
        catch (Exception ex)
        {
            throw ex.InnerException;
        }

    }
}

public class Configurations
{
    private readonly TemplateContext dbContext;


    public Configurations(TemplateContext _dbContext)
    {
        dbContext = _dbContext;
    }

    public async Task SeedData()
    {
        //You seeding code
    }
}`
0
10/6/2019 10:11:50 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow