Skip to content

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

Closed

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)

Activity

  1. reopened this on Nov 15, 2024
  2. ajcvickers commented on Nov 15, 2024

    @ajcvickers
    Contributor

    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.

  3. ajcvickers commented on Nov 29, 2024

    @ajcvickers
    Contributor

    EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

    BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

  4. wqoq commented on Apr 14, 2026

    @wqoq

    @ajcvickers I'm getting this same error with EF 7.0.20, but I can reproduce it with the latest 10.0.6. Looking at the blame on MigrationsModelDiffer.DiffData it hasn't changed at all in that time except for a typo on a variable name.

    See the sample code below. Basically, I have a simple table with an ID number and a text value. The table has some values seeded. The ID column was the primary key, but I later decided to expand the PK to include the Value column as well.

    Use the code below as is, and run dotnet ef migrations add Initial. Then uncomment the line that's commented out, and run dotnet ef migrations add whatever, and the ArgumentOutOfRangeException will get thrown.

    Sample console app to reproduce:

    using Microsoft.EntityFrameworkCore;
    
    namespace EF_HasKey_HasData
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello, World!");
            }
        }
    
        public class MyContext : DbContext
        {
            public DbSet<Item> Items { get; set; }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=example-db;Integrated Security=True");
            }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                // 1. With this configuration, run "dotnet ef migrations add Initial"
                modelBuilder.Entity<Item>().HasKey(x => x.Id).IsClustered(true);
    
                modelBuilder.Entity<Item>().HasData(
                    new Item { Id = 1, Value = "foo" },
                    new Item { Id = 2, Value = "bar" },
                    new Item { Id = 3, Value = "baz" },
                    new Item { Id = 4, Value = "foobar" },
                    new Item { Id = 5, Value = "foobaz" });
    
                // 2. After Initial migration is created, uncomment the line below and run "dotnet ef migrations add blah"
                //modelBuilder.Entity<Item>().HasKey(x => new { x.Id, x.Value }).IsClustered(true);
            }
        }
    
        public class Item
        {
            public short Id { get; set; }
            public required string Value { get; set; }
        }
    }
  5. roji commented on Apr 14, 2026

    @roji
    Member

    AI Triage

    The below is an AI-generated analysis and may contain inaccuracies.

    Summary

    Confirmed bug: adding a migration that expands the primary key of a table with seed data (HasData) throws ArgumentOutOfRangeException in MigrationsModelDiffer.DiffData. For example, changing a PK from {Id} to {Id, Value} on an entity with seeded data causes the crash.

    Root Cause

    In MigrationsModelDiffer.DiffData, the loop iterates up to targetKey.Columns.Count and accesses sourceKey.Columns[i] without checking that i is within bounds of the source key. The column count check (targetKey.Columns.Count != sourceKey.Columns.Count) that would catch mismatched key sizes comes after the loop rather than before it:

    for (var i = 0; i < targetKey.Columns.Count; i++)
    {
        var keyColumn = targetKey.Columns[i];
        var sourceColumn = diffContext.FindSource(keyColumn);
        if (sourceColumn == null
            || sourceKey.Columns[i] !=  throws when target PK has more columnssourceColumn          // 
            || keyColumn.ProviderClrType != sourceColumn.ProviderClrType)
        {
            mappingFound = false;
            break;
        }
    }
    
    if (!mappingFound
        || targetKey.Columns.Count != sourceKey.Columns. this check should come before the loopCount) // 
    {
        tableMapping.Add(targetTable, null);
        continue;
    }

    When the target PK has more columns than the source PK (expanding a key), sourceKey.Columns[i] is accessed with an out-of-range index.

    Regression

    This bug was introduced in commit 0dc7cadca4 ("Add TPC support for data seeding") and has been present since EF Core 7.0 Preview 5. It affects all versions since (7.x, 8.x, 9.x, 10.x).

    Reproduces on

    • EF Core 10.0.6 (latest) with SQLite
    • Confirmed also reproducible on SQL Server per @wqoq's comment
    Minimal repro

    Step 1: Create a console app with the following Program.cs and run dotnet ef migrations add Initial:

    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Logging;
    
    Console.WriteLine("Hello, World!");
    
    public class MyContext : DbContext
    {
        public DbSet<Item> Items { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseSqlite("Data Source=test.db")
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Item>().HasKey(x => x.Id);
    
            modelBuilder.Entity<Item>().HasData(
                new Item { Id = 1, Value = "foo" },
                new Item { Id = 2, Value = "bar" },
                new Item { Id = 3, Value = "baz" });
        }
    }
    
    public class Item
    {
        public int Id { get; set; }
        public required string Value { get; set; }
    }

    Step 2: Add the following line at the end of OnModelCreating to expand the PK, then run dotnet ef migrations add ChangeKey:

    modelBuilder.Entity<Item>().HasKey(x => new { x.Id, x.Value });

    The second migration command will throw ArgumentOutOfRangeException.

    Possible Duplicates

    No duplicates found.

  6. added theissue type on Apr 14, 2026
  7. 3 remaining items

  8. roji commented on Apr 14, 2026

    @roji
  9. reopened this on Apr 14, 2026
  10. changed the title [-]Index was out of range issue while migration[/-] [+]ArgumentOutOfRangeException when adding a migration that expands the primary key of a table with seed data[/+] on Apr 14, 2026
  11. modified the milestones: Backlog, 11.0.0 on Apr 15, 2026
  12. Trigun27 commented on Jun 17, 2026

    @Trigun27

    Unfortunately, I got the same bug.
    I needed to update the keys from simple to composite.

    I wonder will there be a fix for EF Core 10?

  13. AhmedSadek10 commented on Aug 15, 2026

    @AhmedSadek10
    Contributor

    I was able to reproduce this issue and can submit a fix with tests. Is it okay if I work on it? @AndriySvyryd

  14. modified the milestones: 11.0.0, 12.0.0 on Aug 17, 2026
  15. AndriySvyryd commented on Aug 17, 2026

    @AndriySvyryd
    Member
  16. added a commit that references this issue on Sep 10, 2026
    83fc4cb
  17. added a commit that references this issue on Sep 25, 2026
    f07c8da
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions