Repository navigation
Description
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
ajcvickers commentedon Nov 15, 2024
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.
ajcvickers commentedon Nov 29, 2024
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.
wqoq commentedon Apr 14, 2026
@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.DiffDatait 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 rundotnet ef migrations add whatever, and theArgumentOutOfRangeExceptionwill get thrown.Sample console app to reproduce:
roji commentedon Apr 14, 2026
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) throwsArgumentOutOfRangeExceptioninMigrationsModelDiffer.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 totargetKey.Columns.Countand accessessourceKey.Columns[i]without checking thatiis 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: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
Minimal repro
Step 1: Create a console app with the following
Program.csand rundotnet ef migrations add Initial:Step 2: Add the following line at the end of
OnModelCreatingto expand the PK, then rundotnet ef migrations add ChangeKey:The second migration command will throw
ArgumentOutOfRangeException.Possible Duplicates
No duplicates found.
3 remaining items
roji commentedon Apr 14, 2026
[-]Index was out of range issue while migration[/-][+]ArgumentOutOfRangeException when adding a migration that expands the primary key of a table with seed data[/+]Trigun27 commentedon Jun 17, 2026
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?
AhmedSadek10 commentedon Aug 15, 2026
I was able to reproduce this issue and can submit a fix with tests. Is it okay if I work on it? @AndriySvyryd
AndriySvyryd commentedon Aug 17, 2026
@AhmedSadek10 Sure
Fix seed data diffing when primary key column count changes (#38824)
Fix seed data diffing when primary key column count changes (#38824)