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

BulkSaveChanges throws NullReferenceException when saving Many-to-many relationship #1563

Open
SnakeBite94 opened this issue Sep 13, 2024 · 2 comments
Labels

Comments

@SnakeBite94
Copy link

Hello,

when saving an entity with many-to-many relation to another entity (as described here), BulkSaveChanges throws an exception:

System.NullReferenceException: 'Object reference not set to an instance of an object.'
   at EFCore.BulkExtensions.DbContextBulkTransactionSaveChanges.<>c.<SaveChangesAsync>b__2_3(IEntityType e)
   at EFCore.BulkExtensions.DbContextBulkTransactionSaveChanges.<>c__DisplayClass2_0.<SaveChangesAsync>b__6(<>f__AnonymousType1`4 g)
   at Medallion.Collections.TopologicalSorter.TopologicalSort[TElement](ITopologicalSortProvider`1 provider)+MoveNext()
   at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
   at System.Linq.Enumerable.ConcatIterator`1.ToList()
   at EFCore.BulkExtensions.DbContextBulkTransactionSaveChanges.SaveChangesAsync(DbContext context, BulkConfig bulkConfig, Action`1 progress, Boolean isAsync, CancellationToken cancellationToken)
   at EFCore.BulkExtensions.DbContextBulkTransactionSaveChanges.SaveChanges(DbContext context, BulkConfig bulkConfig, Action`1 progress)
   at EFCore.BulkExtensions.DbContextBulkTransaction.Execute[T](DbContext context, Type type, IEnumerable`1 entities, OperationType operationType, BulkConfig bulkConfig, Action`1 progress)
   at EFCore.BulkExtensions.DbContextBulkExtensions.BulkSaveChanges(DbContext context, BulkConfig bulkConfig, Action`1 progress)
...

The code throwing the exception (private static async Task SaveChangesAsync(...))
obrazek

There was a related, possibly same, but closed issue: 1311

We have a scaffolded model, the generated configuration looks like this:

entity.HasMany(d => d.TransportSubSystems)
     .WithMany(p => p.InterruptionAffectedDuties)
     .UsingEntity<Dictionary<string, object>>(
         "InterruptionAffectedDutySubsystem",
         l => l.HasOne<TransportSubSystem>().WithMany().HasForeignKey("SubSystemID").OnDelete(DeleteBehavior.ClientSetNull).HasConstraintName("FK_InterruptionAffectedDutySubsystems_SubSystemID"),
         r => r.HasOne<InterruptionAffectedDuty>().WithMany().HasForeignKey("InterruptionAffectedDutyID").HasConstraintName("FK_InterruptionAffectedDutySubsystems_InterruptionAffectedDuties"),
         j =>
         {
             j.HasKey("InterruptionAffectedDutyID", "SubSystemID");

             j.ToTable("InterruptionAffectedDutySubsystems", "Core");
         });

The UsingEntity<Dictionary<string, object>> is in accordance to the last "Important" note of the "Basic many-to-many" section on MSDN ). We use .net8 and EFCoreSqlServer 8.0.8.

The issue seems to be with the Dictionary used for the many-to-many relationship entity, because it does not have an EntityType for the toplogical sort.

@borisdj
Copy link
Owner

borisdj commented Nov 19, 2024

Can you write entire code with entity class definitions and Context config and also snippet for reproducing the issue.

@SnakeBite94
Copy link
Author

Hello, I have talked to our PM about this issue and he approved me to create the minimal example when there is some free time, hopefully ASAP. Thank you!

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

No branches or pull requests

2 participants