简体   繁体   English

INSERT 语句与 FOREIGN KEY 约束冲突 语句已终止

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

I have a migration seed data that I wish to run when OnModelCreating method runs in my application.OnModelCreating方法在我的应用程序中运行时,我希望运行迁移种子数据。

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部分数据是针对这两个实体的 - StateLGA ,所以除了其他的之外,我还有这两行

protected override void OnModelCreating(ModelBuilder modelBuilder)

method in ApplicationDbContext . 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.我有一个方法Seed()这是一个扩展方法,我希望运行它来为这两个表提供种子。

I call the Seed() method as follows:我调用Seed()方法如下:

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:但是当我运行update-database时,我收到以下错误:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_LGAs_States_StateId". INSERT 语句与 FOREIGN KEY 约束“FK_LGAs_States_StateId”冲突。 The conflict occurred in database...table "dbo.States", column 'Id'.数据库中发生冲突...表“dbo.States”,列“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但是,我意识到,当我检查生成的States表时,我似乎找不到表中的最后一行,即使它在数据种子代码中

I am running ASP.NET Core 2.2 with Entity Framework Core on a Windows 10 OS我在 Windows 10 操作系统上运行带有实体框架核心的 ASP.NET Core 2.2

Where could I be going wrong please?请问我哪里出错了?

Thank you for your assistance.谢谢您的帮助。

Change the FOREIGN KEY constraint name.更改 FOREIGN KEY 约束名称。 It will work.它会起作用的。

call seed function from startup.cs.从 startup.cs 调用种子 function。 Create Configuration class & inject it into Configure function of startup class.创建配置 class 并将其注入到启动 class 的配置 function 中。 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
    }
}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 {“INSERT语句与FOREIGN KEY约束冲突该语句已被终止。”} - {“The INSERT statement conflicted with the FOREIGN KEY constraint The statement has been terminated.”} EntityFramework 6:INSERT语句与FOREIGN KEY约束冲突,但父记录已保存 - EntityFramework 6: The INSERT statement conflicted with the FOREIGN KEY constraint Error but the parent record has been saved BulkInsert:INSERT语句与FOREIGN KEY约束冲突 - BulkInsert: The INSERT statement conflicted with the FOREIGN KEY constraint “ INSERT语句与FOREIGN KEY约束冲突 - "The INSERT statement conflicted with the FOREIGN KEY constraint INSERT语句与FOREIGN KEY约束冲突 - The INSERT statement conflicted with the FOREIGN KEY constraint SqlException:INSERT语句与FOREIGN KEY约束冲突 - SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint 错误:INSERT语句与FOREIGN KEY约束冲突 - Error : INSERT statement conflicted with the FOREIGN KEY constraint INSERT语句与FOREIGN KEY约束错误冲突 - The INSERT statement conflicted with the FOREIGN KEY constraint error SqlException INSERT语句与FOREIGN KEY约束冲突 - SqlException The INSERT statement conflicted with the FOREIGN KEY constraint INSERT语句与MVC中的FOREIGN KEY约束冲突 - The INSERT statement conflicted with the FOREIGN KEY constraint in MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM