Skip to content

Conversation

@hikalkan
Copy link
Member

@hikalkan hikalkan commented Aug 20, 2025

See https://learn.microsoft.com/en-us/ef/core/modeling/entity-types?tabs=data-annotations#shared-type-entity-types

Basically, DbContext.Set<T>() has an overload that takes a string name (the entity name). In this way, we can map the same entity type to multiple database tables and configure seperately.

With this PR, now we can use Repository.SetCustomEntityName() method to indicate the entity name, so it gets the right DbSet and work on the right database table. This method should be called first, then any repository operation will use it. It is also possible to change it later on the same object and use other database tables. Interesting feature. I will implement it later for MongoDB (so we can switch to another database collection for the same entity type).

Introduces the ability to set a custom entity name for repositories by adding a SetCustomEntityName method to IRepository and BasicRepositoryBase. Updates EfCoreRepository to use the custom entity name when accessing DbSet, and extends IEfCoreDbContext with an overload of Set<T> that accepts a name. Test stubs are updated to implement the new method.
@hikalkan
Copy link
Member Author

@maliming please review and add tests for this

@hikalkan hikalkan marked this pull request as ready for review August 20, 2025 14:18
@maliming maliming merged commit f4b6b51 into dev Aug 21, 2025
3 checks passed
@maliming maliming deleted the ef-core-shared-entity-types branch August 21, 2025 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants