Skip to content

Fix table metadata collection for keyless entities#77

Merged
PhenX merged 2 commits intoPhenX:mainfrom
Eisfunke:eisfunke/fix-keyless-entities
Oct 30, 2025
Merged

Fix table metadata collection for keyless entities#77
PhenX merged 2 commits intoPhenX:mainfrom
Eisfunke:eisfunke/fix-keyless-entities

Conversation

@Eisfunke
Copy link
Copy Markdown
Contributor

We have an EF Core application with a keyless ([Keyless] annotation) entry that represents a database view. When trying to use BulkInsert, I always get this error, regardless of what entity type I'm trying to insert:

System.InvalidOperationException: Cannot determine table name.
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at PhenX.EntityFrameworkCore.BulkInsert.Metadata.MetadataProvider.GetTables(DbContext context)
   at PhenX.EntityFrameworkCore.BulkInsert.Metadata.MetadataProvider.GetTableInfo[T](DbContext context)
   at PhenX.EntityFrameworkCore.BulkInsert.Extensions.PublicExtensions.ExecuteBulkInsertAsync[T,TOptions](DbSet`1 dbSet, IEnumerable`1 entities, Action`1 configure, OnConflictOptions`1 onConflict, CancellationToken cancellationToken)
   at PhenX.EntityFrameworkCore.BulkInsert.Extensions.PublicExtensions.ExecuteBulkInsertAsync[T,TConfig](DbContext dbContext, IEnumerable`1 entities, Action`1 configure, OnConflictOptions`1 onConflict, CancellationToken cancellationToken)
   at PhenX.EntityFrameworkCore.BulkInsert.Extensions.PublicExtensions.ExecuteBulkInsertAsync[T](DbContext dbContext, IEnumerable`1 entities, OnConflictOptions`1 onConflict, CancellationToken cancellationToken)
   ...

This seems to be caused in GetTables, which tries to create TableMetadatas for all entity types. However, when any of these entity types don't have an associated table name, the TableMetadata constructor will throw the above System.InvalidOperationException: Cannot determine table name.. This is the case for the keyless entity in our codebase, GetTableName returns null here.

This seems to be fixable by simply filtering out entities where the table name is null. I tried it for our usecase, and it seems to work, although I don't know whether this has any repercussions on the rest of the library, although I hope it doesn't, because keyless entities can't ever be inserted anyway, so it shouldn't be relevant for the lib. See https://learn.microsoft.com/en-us/ef/core/modeling/keyless-entity-types

@Eisfunke Eisfunke requested a review from PhenX as a code owner October 27, 2025 14:21
@PhenX
Copy link
Copy Markdown
Owner

PhenX commented Oct 27, 2025

Thanks for your contribution, I have no doubt it fixes the issue, but can you add a keyless entity in the test DbContext please, to be sure the issue won't come back ?

@Eisfunke
Copy link
Copy Markdown
Contributor Author

Thanks for your contribution, I have no doubt it fixes the issue, but can you add a keyless entity in the test DbContext please, to be sure the issue won't come back ?

Hey! Thanks for the feedback. I added the entity to the context. Without the fix, the tests fail now, but with my fix they run fine. Although I don't use the entity for anything in the tests so far. Is that okay for you like that?

@PhenX PhenX merged commit 0cdcce7 into PhenX:main Oct 30, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants