'Entity Framework Core Migration Seed throw Duplication Error

I am working with .NET 6 and an Entity Framework 7 solution. Part of migration, I have created a Seed extension classes to insert the data. My primary keys are of type Guid that I am inserting with object to insert. Once of my class has about 70 seed data and I am constantly getting duplication error on different Ids where I double check there is no duplicate of Guid Id.

I come across migrationBuilder.UpdateData() but not sure if is right? And second how to apply within my code?

The Add-Migration [xx] works fine, error is thrown when running update-database.

I have notice after Add-Migration, the migration class insert data of same object into 2 part with split of record but duplicate one of the record that throws the error

 migrationBuilder.InsertData(
            schema: "dbo",
            table: "DataReadingRule",
            columns: new[] { "DataReadingRuleId", "EndFieldTo", "FieldName", "FieldTxtJustity", "FieldWidth", "ImportFileDefinationId", "IsRequire", "Multiplier", "Notes", "RowIndex", "StartFieldFrom", "TableTitle", "TxtFieldFormat" },
            values: new object[,]
            {

This is the error:

Violation of PRIMARY KEY constraint 'PK_DataReadingRule'. Cannot insert duplicate key in object 'dbo.DataReadingRule'. The duplicate key value is (xxxxxxxx-7730-4da0-b987-439a81bxxxxx).
The statement has been terminated.

Entity class:

public class DataReadingRule
{
    public Guid DataReadingRuleId { get; set; }
    public Guid ImportFileDefinationId { get; set; }
    public int RowIndex { get; set; }
    public string TableTitle { get; set; } 
    public string FieldName { get; set; }
    public DataRequire IsRequire { get; set; }
    public int FieldWidth { get; set; }
    public int StartFieldFrom { get; set; }
    public int EndFieldTo { get; set; }
    public TxtJustity FieldTxtJustity { get; set; }
    public TextFieldFormat TxtFieldFormat { get; set; }
    public int Multiplier { get; set; }
    public string Notes { get; set; } 
    public ImportFileDefination ImportFileDefination { get; set; }
    public DataMappingRule DataMappingRule { get; set; }
}

Seed extension method:

public static class DataReadingRuleSeed
{
    public static void Seed(this ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<DataReadingRule>()
            .HasData(
                new DataReadingRule { DataReadingRuleId = Guid.Parse("xxxxxxxx-7730-4da0-b987-439a81bf9xxx"), ImportFileDefinationId = Guid.Parse("xxxxxxxx-18f6-48df-aac6-f248952xxxxx"), RowIndex = 1, TableTitle = "", FieldName = "", IsRequire = DataRequire.Optional, FieldWidth = 0, StartFieldFrom = 0, EndFieldTo = 0, FieldTxtJustity = TxtJustity.NotProvided, TxtFieldFormat = TextFieldFormat.NotProvided, Multiplier = 0, Notes = "" },
 .....
    );
  }
}

DbContext class:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
     DataReadingRuleSeed.Seed(modelBuilder);
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source