Skip to content

ArgumentOutOfRangeException when adding a migration that expands the primary key of a table with seed data #35116

@mtapis

Description

@mtapis

I am working on a project that will run multitenants with multi and single database, for that i need to add TenantId to all my entities, entities with guid primar keys i had no problem , but when i add tenantId columns to entities with composite keys such as (code,batch) => code, batch,tenantId,
i apply those configurations to related entities with this ones . After that when i try to add migration it does not work i get this error ;

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.SZArrayHelper.get_Item[T](Int32 index)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffData(IRelationalModel source, IRelationalModel target, DiffContext diffContext)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDataOperations(IRelationalModel source, IRelationalModel target, DiffContext diffContext)+MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

Here is some of my code

    public void Configure(EntityTypeBuilder<Referans> builder)
    {
        builder.ToTable("Referans");
        builder.HasKey(x => new { x.Kod, x.TipId ,x.TenantId}); // Tip Id ve Kod composite key olarak güncellendi.
        builder.Property(x => x.Kod).IsRequired().HasMaxLength(20);
        builder.Property(x => x.TipId).IsRequired();
        builder.Property(x => x.Aciklama1).HasMaxLength(200);
        builder.Property(x => x.Aciklama2).HasMaxLength(200);
        builder.Property(x => x.GrupKod).HasMaxLength(20);
        builder.Property(x => x.RowVersion).IsRowVersion();
        builder.HasOne(k => k.Tip).WithMany(k => k.Referanslar).HasForeignKey(x => new { x.TipId ,x.TenantId}).IsRequired().OnDelete(DeleteBehavior.NoAction);
    }

    public void Configure(EntityTypeBuilder<Banka> builder)
    {
        builder.ToTable("Banka");
        builder.Property(k => k.Kod).HasMaxLength(3);
        builder.Property(k => k.Ad).IsRequired().HasMaxLength(100);
        builder.Property(k => k.SwiftKodu).HasMaxLength(8);
        builder.HasOne(k => k.Ulke).WithMany(c => c.Bankalar).HasForeignKey(k => new { k.UlkeNumKod ,k.TenantId }).IsRequired()
            .OnDelete(DeleteBehavior.NoAction);
    }
    public void Configure(EntityTypeBuilder<CariAdres> builder)
    {
        builder.ToTable("CariAdres");
        builder.Property(x => x.CariID).IsRequired();
        builder.Property(x => x.AdresBaslik).HasMaxLength(50);
        builder.Property(x => x.UlkeNumKod).IsRequired().HasMaxLength(3);
        builder.Property(x => x.SehirKod).IsRequired().HasMaxLength(3);
        builder.Property(x => x.IlceKod).IsRequired().HasMaxLength(5);
        builder.Property(x => x.Mahalle).HasMaxLength(40);
        builder.Property(x => x.KasabaKoy).HasMaxLength(40);
        builder.Property(x => x.Cadde).HasMaxLength(40);
        builder.Property(x => x.BinaAdi).HasMaxLength(40);
        builder.Property(x => x.DisKapiNo).HasMaxLength(10);
        builder.Property(x => x.IcKapiNo).HasMaxLength(10);
        builder.Property(x => x.PostaKod).HasMaxLength(7);
        builder.Property(x => x.SecurityId).HasMaxLength(2);

        //Bağlantılar
        builder.HasOne(c => c.Cari).WithMany(r => r.CariAdresler).OnDelete(DeleteBehavior.NoAction);
        builder.HasOne(c => c.Ulke).WithMany().HasForeignKey(c => new { c.UlkeNumKod, c.TenantId }).OnDelete(DeleteBehavior.NoAction);
        builder.HasOne(c => c.Sehir).WithMany().HasForeignKey(c => new { c.SehirKod, c.TenantId }).OnDelete(DeleteBehavior.NoAction);
        builder.HasOne(c => c.Ilce).WithMany().HasForeignKey(c => new { c.IlceKod, c.TenantId }).OnDelete(DeleteBehavior.NoAction);

    }


        private void SeedOdemeSekilleriReferanslar(ModelBuilder modelBuilder)
        {
            List<Referans> referanslar = new();
            string xmlDosyasi = _configuration.GetSection(AppSettingsReferencesFilePathConst.Name)[AppSettingsReferencesFilePathConst.OdemeSekilleriPath];
            XmlDocument xmlDocument = new();
            xmlDocument.Load(xmlDosyasi);
            var odemeSekilleri = xmlDocument.SelectNodes("/OdemeSekilleri/OdemeSekli");

            foreach (XmlNode odemeSekli in odemeSekilleri)
            {
                referanslar.Add(new Referans()
                {
                    Kod = odemeSekli["kod"].InnerText,
                    Aciklama1 = odemeSekli["Aciklama"].InnerText,
                    TipId = (int)SistemReferansTipleri.UNEDIFACT4461OdemeCesitleriKodListesi,
                    TenantId = Guid.Empty
                });
            }
            modelBuilder.Entity<Referans>().HasData(referanslar);
        }

EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 8.0)

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions