Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index was out of range issue while migration #35116

Open
mtapis opened this issue Nov 15, 2024 · 1 comment
Open

Index was out of range issue while migration #35116

mtapis opened this issue Nov 15, 2024 · 1 comment

Comments

@mtapis
Copy link

mtapis commented Nov 15, 2024

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)

@ajcvickers
Copy link
Member

This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants