diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs index cd85acce50..1d058d19b9 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs @@ -43,6 +43,13 @@ public abstract class BasicRepositoryBase : public IEntityChangeTrackingProvider EntityChangeTrackingProvider => LazyServiceProvider.LazyGetRequiredService(); public bool? IsChangeTrackingEnabled { get; protected set; } + + protected string? CustomEntityName { get; private set; } + + public void SetCustomEntityName(string? name) + { + CustomEntityName = name; + } protected BasicRepositoryBase() { diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs index dc39255b25..c5942088f0 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs @@ -13,6 +13,8 @@ namespace Volo.Abp.Domain.Repositories; public interface IRepository { bool? IsChangeTrackingEnabled { get; } + + void SetCustomEntityName(string? name); } public interface IRepository : IReadOnlyRepository, IBasicRepository diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs index 0ed9a2e199..760660dc82 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs @@ -61,7 +61,7 @@ public class EfCoreRepository : RepositoryBase, IE } [Obsolete("Use GetDbSetAsync() method.")] - public virtual DbSet DbSet => DbContext.Set(); + public virtual DbSet DbSet => GetDbSetInternal(DbContext); Task> IEfCoreRepository.GetDbSetAsync() { @@ -70,7 +70,7 @@ public class EfCoreRepository : RepositoryBase, IE protected async Task> GetDbSetAsync() { - return (await GetDbContextAsync()).Set(); + return GetDbSetInternal(await GetDbContextAsync()); } protected async Task GetDbConnectionAsync() @@ -110,7 +110,7 @@ public class EfCoreRepository : RepositoryBase, IE var dbContext = await GetDbContextAsync(); - var savedEntity = (await dbContext.Set().AddAsync(entity, GetCancellationToken(cancellationToken))).Entity; + var savedEntity = (await GetDbSetInternal(dbContext).AddAsync(entity, GetCancellationToken(cancellationToken))).Entity; if (autoSave) { @@ -120,6 +120,13 @@ public class EfCoreRepository : RepositoryBase, IE return savedEntity; } + private DbSet GetDbSetInternal(TDbContext dbContext) + { + return CustomEntityName != null + ? dbContext.Set(CustomEntityName) + : dbContext.Set(); + } + public async override Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) { var entityArray = entities.ToArray(); @@ -147,7 +154,7 @@ public class EfCoreRepository : RepositoryBase, IE return; } - await dbContext.Set().AddRangeAsync(entityArray, cancellationToken); + await GetDbSetInternal(dbContext).AddRangeAsync(entityArray, cancellationToken); if (autoSave) { @@ -159,9 +166,10 @@ public class EfCoreRepository : RepositoryBase, IE { var dbContext = await GetDbContextAsync(); - if (dbContext.Set().Local.All(e => e != entity)) + var dbSet = GetDbSetInternal(dbContext); + if (dbSet.Local.All(e => e != entity)) { - dbContext.Set().Attach(entity); + dbSet.Attach(entity); dbContext.Update(entity); } @@ -197,7 +205,7 @@ public class EfCoreRepository : RepositoryBase, IE var dbContext = await GetDbContextAsync(); - dbContext.Set().UpdateRange(entityArray); + GetDbSetInternal(dbContext).UpdateRange(entityArray); if (autoSave) { @@ -209,7 +217,7 @@ public class EfCoreRepository : RepositoryBase, IE { var dbContext = await GetDbContextAsync(); - dbContext.Set().Remove(entity); + GetDbSetInternal(dbContext).Remove(entity); if (autoSave) { @@ -318,7 +326,7 @@ public class EfCoreRepository : RepositoryBase, IE public async override Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default) { var dbContext = await GetDbContextAsync(); - var dbSet = dbContext.Set(); + var dbSet = GetDbSetInternal(dbContext); var entities = await dbSet .Where(predicate) @@ -335,8 +343,9 @@ public class EfCoreRepository : RepositoryBase, IE public async override Task DeleteDirectAsync(Expression> predicate, CancellationToken cancellationToken = default) { var dbContext = await GetDbContextAsync(); - var dbSet = dbContext.Set(); - await dbSet.Where(predicate).ExecuteDeleteAsync(GetCancellationToken(cancellationToken)); + await GetDbSetInternal(dbContext) + .Where(predicate) + .ExecuteDeleteAsync(GetCancellationToken(cancellationToken)); } public virtual async Task EnsureCollectionLoadedAsync( diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContext.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContext.cs index 833163d223..3097eba9cb 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContext.cs @@ -31,6 +31,9 @@ public interface IEfCoreDbContext : IDisposable, IInfrastructure Set() where T : class; + + DbSet Set(string name) + where T : class; DatabaseFacade Database { get; } diff --git a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs index 4a7a9fb28f..63aa0e3411 100644 --- a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs +++ b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs @@ -408,6 +408,11 @@ public class RepositoryRegistration_Tests public class MyTestAggregateRootWithDefaultPkEmptyRepository : IMyTestAggregateRootWithDefaultPkEmptyRepository { public bool? IsChangeTrackingEnabled { get; set; } + + public void SetCustomEntityName(string name) + { + + } } public class TestDbContextRegistrationOptions : AbpCommonDbContextRegistrationOptions