diff --git a/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk.EntityFrameworkCore.csproj b/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk.EntityFrameworkCore.csproj
index 857a6a409c..9a42aec87b 100644
--- a/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk.EntityFrameworkCore.csproj
+++ b/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk.EntityFrameworkCore.csproj
@@ -12,6 +12,7 @@
+
diff --git a/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbContext.cs b/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbContext.cs
index b523edddc9..82e3e582b0 100644
--- a/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbContext.cs
+++ b/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbContext.cs
@@ -2,14 +2,20 @@
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
+using Volo.Abp.MultiTenancy;
+using Volo.Abp.MultiTenancy.EntityFrameworkCore;
namespace AbpDesk.EntityFrameworkCore
{
[ConnectionStringName(ConnectionStrings.DefaultConnectionStringName)] //Explicitly declares this module always uses the default connection string
- public class AbpDeskDbContext : AbpDbContext
+ public class AbpDeskDbContext : AbpDbContext, IMultiTenancyDbContext
{
public DbSet Tickets { get; set; }
+ public DbSet Tenants { get; set; }
+
+ public DbSet TenantConnectionStrings { get; set; }
+
public AbpDeskDbContext(DbContextOptions options)
: base(options)
{
@@ -20,6 +26,8 @@ namespace AbpDesk.EntityFrameworkCore
{
base.OnModelCreating(modelBuilder);
+ this.ConfigureAbpMultiTenancy(modelBuilder);
+
//Use different classes to map each entity type?
modelBuilder.Entity(b =>
{
diff --git a/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskEntityFrameworkCoreModule.cs b/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskEntityFrameworkCoreModule.cs
index 3538a2186e..6c3c22d53e 100644
--- a/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskEntityFrameworkCoreModule.cs
+++ b/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskEntityFrameworkCoreModule.cs
@@ -1,17 +1,23 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Modularity;
+using Volo.Abp.MultiTenancy.EntityFrameworkCore;
namespace AbpDesk.EntityFrameworkCore
{
- [DependsOn(typeof(AbpDeskDomainModule), typeof(AbpEntityFrameworkCoreModule))]
+ [DependsOn(
+ typeof(AbpDeskDomainModule),
+ typeof(AbpEntityFrameworkCoreModule),
+ typeof(AbpMultiTenancyEntityFrameworkCoreModule)
+ )]
public class AbpDeskEntityFrameworkCoreModule : AbpModule
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddAbpDbContext(options =>
{
- options.WithDefaultRepositories();
+ options.AddDefaultRepositories();
+ options.ReplaceDbContext();
});
services.AddAssemblyOf();
diff --git a/src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/20180105184326_MultiTenancyModuleAdded.Designer.cs b/src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/20180105184326_MultiTenancyModuleAdded.Designer.cs
new file mode 100644
index 0000000000..087141c5f3
--- /dev/null
+++ b/src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/20180105184326_MultiTenancyModuleAdded.Designer.cs
@@ -0,0 +1,92 @@
+//
+using AbpDesk.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage;
+using Microsoft.EntityFrameworkCore.Storage.Internal;
+using System;
+
+namespace AbpDesk.EntityFrameworkCore.Migrations
+{
+ [DbContext(typeof(AbpDeskDbContext))]
+ [Migration("20180105184326_MultiTenancyModuleAdded")]
+ partial class MultiTenancyModuleAdded
+ {
+ protected override void BuildTargetModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("ProductVersion", "2.0.0-rtm-26452")
+ .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
+
+ modelBuilder.Entity("AbpDesk.Tickets.Ticket", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd();
+
+ b.Property("Body")
+ .HasMaxLength(65536);
+
+ b.Property("ConcurrencyStamp")
+ .IsConcurrencyToken();
+
+ b.Property("Title")
+ .IsRequired()
+ .HasMaxLength(256);
+
+ b.HasKey("Id");
+
+ b.ToTable("DskTickets");
+ });
+
+ modelBuilder.Entity("Volo.Abp.MultiTenancy.Tenant", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd();
+
+ b.Property("Name")
+ .IsRequired()
+ .HasMaxLength(64);
+
+ b.HasKey("Id");
+
+ b.HasIndex("Name");
+
+ b.ToTable("MtTenants");
+ });
+
+ modelBuilder.Entity("Volo.Abp.MultiTenancy.TenantConnectionString", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd();
+
+ b.Property("Name")
+ .IsRequired()
+ .HasMaxLength(256);
+
+ b.Property("TenantId");
+
+ b.Property("Value")
+ .IsRequired()
+ .HasMaxLength(1024);
+
+ b.HasKey("Id");
+
+ b.HasIndex("TenantId");
+
+ b.ToTable("MtTenantConnectionStrings");
+ });
+
+ modelBuilder.Entity("Volo.Abp.MultiTenancy.TenantConnectionString", b =>
+ {
+ b.HasOne("Volo.Abp.MultiTenancy.Tenant")
+ .WithMany("ConnectionStrings")
+ .HasForeignKey("TenantId")
+ .OnDelete(DeleteBehavior.Cascade);
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/20180105184326_MultiTenancyModuleAdded.cs b/src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/20180105184326_MultiTenancyModuleAdded.cs
new file mode 100644
index 0000000000..3f9eae2ed0
--- /dev/null
+++ b/src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/20180105184326_MultiTenancyModuleAdded.cs
@@ -0,0 +1,63 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+using System;
+using System.Collections.Generic;
+
+namespace AbpDesk.EntityFrameworkCore.Migrations
+{
+ public partial class MultiTenancyModuleAdded : Migration
+ {
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.CreateTable(
+ name: "MtTenants",
+ columns: table => new
+ {
+ Id = table.Column(type: "uniqueidentifier", nullable: false),
+ Name = table.Column(type: "nvarchar(64)", maxLength: 64, nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_MtTenants", x => x.Id);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "MtTenantConnectionStrings",
+ columns: table => new
+ {
+ Id = table.Column(type: "uniqueidentifier", nullable: false),
+ Name = table.Column(type: "nvarchar(256)", maxLength: 256, nullable: false),
+ TenantId = table.Column(type: "uniqueidentifier", nullable: false),
+ Value = table.Column(type: "nvarchar(1024)", maxLength: 1024, nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_MtTenantConnectionStrings", x => x.Id);
+ table.ForeignKey(
+ name: "FK_MtTenantConnectionStrings_MtTenants_TenantId",
+ column: x => x.TenantId,
+ principalTable: "MtTenants",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateIndex(
+ name: "IX_MtTenantConnectionStrings_TenantId",
+ table: "MtTenantConnectionStrings",
+ column: "TenantId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_MtTenants_Name",
+ table: "MtTenants",
+ column: "Name");
+ }
+
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropTable(
+ name: "MtTenantConnectionStrings");
+
+ migrationBuilder.DropTable(
+ name: "MtTenants");
+ }
+ }
+}
diff --git a/src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/AbpDeskDbContextModelSnapshot.cs b/src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/AbpDeskDbContextModelSnapshot.cs
index b84a4c0fca..d355af3c6a 100644
--- a/src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/AbpDeskDbContextModelSnapshot.cs
+++ b/src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/AbpDeskDbContextModelSnapshot.cs
@@ -1,6 +1,12 @@
-using Microsoft.EntityFrameworkCore;
+//
+using AbpDesk.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage;
+using Microsoft.EntityFrameworkCore.Storage.Internal;
+using System;
namespace AbpDesk.EntityFrameworkCore.Migrations
{
@@ -9,8 +15,9 @@ namespace AbpDesk.EntityFrameworkCore.Migrations
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
+#pragma warning disable 612, 618
modelBuilder
- .HasAnnotation("ProductVersion", "1.1.0-rtm-22752")
+ .HasAnnotation("ProductVersion", "2.0.0-rtm-26452")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("AbpDesk.Tickets.Ticket", b =>
@@ -32,6 +39,53 @@ namespace AbpDesk.EntityFrameworkCore.Migrations
b.ToTable("DskTickets");
});
+
+ modelBuilder.Entity("Volo.Abp.MultiTenancy.Tenant", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd();
+
+ b.Property("Name")
+ .IsRequired()
+ .HasMaxLength(64);
+
+ b.HasKey("Id");
+
+ b.HasIndex("Name");
+
+ b.ToTable("MtTenants");
+ });
+
+ modelBuilder.Entity("Volo.Abp.MultiTenancy.TenantConnectionString", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd();
+
+ b.Property("Name")
+ .IsRequired()
+ .HasMaxLength(256);
+
+ b.Property("TenantId");
+
+ b.Property("Value")
+ .IsRequired()
+ .HasMaxLength(1024);
+
+ b.HasKey("Id");
+
+ b.HasIndex("TenantId");
+
+ b.ToTable("MtTenantConnectionStrings");
+ });
+
+ modelBuilder.Entity("Volo.Abp.MultiTenancy.TenantConnectionString", b =>
+ {
+ b.HasOne("Volo.Abp.MultiTenancy.Tenant")
+ .WithMany("ConnectionStrings")
+ .HasForeignKey("TenantId")
+ .OnDelete(DeleteBehavior.Cascade);
+ });
+#pragma warning restore 612, 618
}
}
}
diff --git a/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoBlogModule.cs b/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoBlogModule.cs
index 5154184d51..8e5df159d1 100644
--- a/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoBlogModule.cs
+++ b/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoBlogModule.cs
@@ -20,7 +20,7 @@ namespace AbpDesk.Blogging
{
services.AddMongoDbContext(options =>
{
- options.WithDefaultRepositories();
+ options.AddDefaultRepositories();
});
services.Configure(options =>
diff --git a/src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll b/src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll
index 207e4f6c07..879b5f90bd 100644
Binary files a/src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll and b/src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll differ
diff --git a/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs b/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs
index 1e7943591b..99f087c54d 100644
--- a/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs
+++ b/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs
@@ -12,8 +12,12 @@ namespace Volo.Abp.DependencyInjection
///
public abstract class CommonDbContextRegistrationOptions : ICommonDbContextRegistrationOptionsBuilder
{
- public bool SpecifiedDefaultRepositoryTypes => DefaultRepositoryImplementationType != null && DefaultRepositoryImplementationTypeWithDefaultPrimaryKey != null;
-
+ public Type OriginalDbContextType { get; }
+
+ public List ReplacedDbContextTypes { get; }
+
+ public Type DefaultRepositoryDbContextType { get; protected set; }
+
public Type DefaultRepositoryImplementationType { get; private set; }
public Type DefaultRepositoryImplementationTypeWithDefaultPrimaryKey { get; private set; }
@@ -24,12 +28,34 @@ namespace Volo.Abp.DependencyInjection
public Dictionary CustomRepositories { get; }
- public CommonDbContextRegistrationOptions()
+ public bool SpecifiedDefaultRepositoryTypes => DefaultRepositoryImplementationType != null && DefaultRepositoryImplementationTypeWithDefaultPrimaryKey != null;
+
+ protected CommonDbContextRegistrationOptions(Type originalDbContextType)
{
+ OriginalDbContextType = originalDbContextType;
+ DefaultRepositoryDbContextType = originalDbContextType;
CustomRepositories = new Dictionary();
+ ReplacedDbContextTypes = new List();
+ }
+
+ public ICommonDbContextRegistrationOptionsBuilder ReplaceDbContext()
+ {
+ return ReplaceDbContext(typeof(TOtherDbContext));
+ }
+
+ public ICommonDbContextRegistrationOptionsBuilder ReplaceDbContext(Type otherDbContextType)
+ {
+ if (!otherDbContextType.IsAssignableFrom(OriginalDbContextType))
+ {
+ throw new AbpException($"{OriginalDbContextType.AssemblyQualifiedName} should inherit/implement {otherDbContextType.AssemblyQualifiedName}!");
+ }
+
+ ReplacedDbContextTypes.Add(otherDbContextType);
+
+ return this;
}
- public ICommonDbContextRegistrationOptionsBuilder WithDefaultRepositories(bool includeAllEntities = false)
+ public ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(bool includeAllEntities = false)
{
RegisterDefaultRepositories = true;
IncludeAllEntitiesForDefaultRepositories = includeAllEntities;
@@ -37,14 +63,31 @@ namespace Volo.Abp.DependencyInjection
return this;
}
- public ICommonDbContextRegistrationOptionsBuilder WithCustomRepository()
+ public ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(bool includeAllEntities = false)
+ {
+ return AddDefaultRepositories(typeof(TDefaultRepositoryDbContext), includeAllEntities);
+ }
+
+ public ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(Type defaultRepositoryDbContextType, bool includeAllEntities = false)
+ {
+ if (!defaultRepositoryDbContextType.IsAssignableFrom(OriginalDbContextType))
+ {
+ throw new AbpException($"{OriginalDbContextType.AssemblyQualifiedName} should inherit/implement {defaultRepositoryDbContextType.AssemblyQualifiedName}!");
+ }
+
+ DefaultRepositoryDbContextType = defaultRepositoryDbContextType;
+
+ return this;
+ }
+
+ public ICommonDbContextRegistrationOptionsBuilder AddCustomRepository()
{
WithCustomRepository(typeof(TEntity), typeof(TRepository));
return this;
}
- public ICommonDbContextRegistrationOptionsBuilder WithDefaultRepositoryClasses([NotNull] Type repositoryImplementationType, [NotNull] Type repositoryImplementationTypeWithDefaultPrimaryKey)
+ public ICommonDbContextRegistrationOptionsBuilder SetDefaultRepositoryClasses([NotNull] Type repositoryImplementationType, [NotNull] Type repositoryImplementationTypeWithDefaultPrimaryKey)
{
Check.NotNull(repositoryImplementationType, nameof(repositoryImplementationType));
Check.NotNull(repositoryImplementationTypeWithDefaultPrimaryKey, nameof(repositoryImplementationTypeWithDefaultPrimaryKey));
diff --git a/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/ICommonDbContextRegistrationOptionsBuilder.cs b/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/ICommonDbContextRegistrationOptionsBuilder.cs
index 5528ca8b19..167fbcad47 100644
--- a/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/ICommonDbContextRegistrationOptionsBuilder.cs
+++ b/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/ICommonDbContextRegistrationOptionsBuilder.cs
@@ -11,7 +11,29 @@ namespace Volo.Abp.DependencyInjection
/// Registers repositories only for aggregate root entities by default.
/// set to true to include all entities.
///
- ICommonDbContextRegistrationOptionsBuilder WithDefaultRepositories(bool includeAllEntities = false);
+ ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(bool includeAllEntities = false);
+
+ ///
+ /// Registers default repositories for this DbContext.
+ /// Default repositories will use given .
+ ///
+ /// DbContext type that will be used by default repositories
+ ///
+ /// Registers repositories only for aggregate root entities by default.
+ /// set to true to include all entities.
+ ///
+ ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(bool includeAllEntities = false);
+
+ ///
+ /// Registers default repositories for this DbContext.
+ /// Default repositories will use given .
+ ///
+ /// DbContext type that will be used by default repositories
+ ///
+ /// Registers repositories only for aggregate root entities by default.
+ /// set to true to include all entities.
+ ///
+ ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(Type defaultRepositoryDbContextType, bool includeAllEntities = false);
///
/// Registers custom repository for a specific entity.
@@ -19,7 +41,7 @@ namespace Volo.Abp.DependencyInjection
///
/// Entity type
/// Repository type
- ICommonDbContextRegistrationOptionsBuilder WithCustomRepository();
+ ICommonDbContextRegistrationOptionsBuilder AddCustomRepository();
///
/// Uses given class(es) for default repositories.
@@ -27,6 +49,18 @@ namespace Volo.Abp.DependencyInjection
/// Repository implementation type
/// Repository implementation type for default primary key type ()
///
- ICommonDbContextRegistrationOptionsBuilder WithDefaultRepositoryClasses(Type repositoryImplementationType, Type repositoryImplementationTypeWithDefaultPrimaryKey);
+ ICommonDbContextRegistrationOptionsBuilder SetDefaultRepositoryClasses(Type repositoryImplementationType, Type repositoryImplementationTypeWithDefaultPrimaryKey);
+
+ ///
+ /// Replaces given DbContext type with this DbContext type.
+ ///
+ /// The DbContext type to be replaced
+ ICommonDbContextRegistrationOptionsBuilder ReplaceDbContext();
+
+ ///
+ /// Replaces given DbContext type with this DbContext type.
+ ///
+ /// The DbContext type to be replaced
+ ICommonDbContextRegistrationOptionsBuilder ReplaceDbContext(Type otherDbContextType);
}
}
\ No newline at end of file
diff --git a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs
index 6c2edc1f18..ea3e1963f8 100644
--- a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs
+++ b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
-using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Reflection;
@@ -18,7 +17,7 @@ namespace Volo.Abp.Domain.Repositories
Options = options;
}
- public virtual void AddRepositories(IServiceCollection services, Type dbContextType)
+ public virtual void AddRepositories(IServiceCollection services)
{
foreach (var customRepository in Options.CustomRepositories)
{
@@ -27,24 +26,24 @@ namespace Volo.Abp.Domain.Repositories
if (Options.RegisterDefaultRepositories)
{
- RegisterDefaultRepositories(services, dbContextType);
+ RegisterDefaultRepositories(services);
}
}
- protected virtual void RegisterDefaultRepositories(IServiceCollection services, Type dbContextType)
+ protected virtual void RegisterDefaultRepositories(IServiceCollection services)
{
- foreach (var entityType in GetEntityTypes(dbContextType))
+ foreach (var entityType in GetEntityTypes(Options.DefaultRepositoryDbContextType))
{
if (!ShouldRegisterDefaultRepositoryFor(entityType))
{
continue;
}
- RegisterDefaultRepository(services, dbContextType, entityType);
+ RegisterDefaultRepository(services, entityType);
}
}
- protected void RegisterDefaultRepository(IServiceCollection services, Type dbContextType, Type entityType)
+ protected void RegisterDefaultRepository(IServiceCollection services, Type entityType)
{
var primaryKeyType = EntityHelper.GetPrimaryKeyType(entityType);
var isDefaultPrimaryKey = primaryKeyType == typeof(Guid);
@@ -59,8 +58,8 @@ namespace Volo.Abp.Domain.Repositories
else
{
repositoryImplementationType = isDefaultPrimaryKey
- ? GetRepositoryTypeForDefaultPk(dbContextType, entityType)
- : GetRepositoryType(dbContextType, entityType, primaryKeyType);
+ ? GetRepositoryTypeForDefaultPk(Options.DefaultRepositoryDbContextType, entityType)
+ : GetRepositoryType(Options.DefaultRepositoryDbContextType, entityType, primaryKeyType);
}
services.AddDefaultRepository(entityType, repositoryImplementationType);
diff --git a/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs b/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs
index 06a09c8075..1236f17b2f 100644
--- a/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs
+++ b/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs
@@ -7,19 +7,31 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class AbpEfCoreServiceCollectionExtensions
{
- public static IServiceCollection AddAbpDbContext(this IServiceCollection services, Action optionsBuilder = null) //Created overload instead of default parameter
+ public static IServiceCollection AddAbpDbContext(
+ this IServiceCollection services,
+ Action optionsBuilder = null)
where TDbContext : AbpDbContext
{
services.AddMemoryCache();
+ var options = new AbpDbContextRegistrationOptions(typeof(TDbContext));
+ optionsBuilder?.Invoke(options);
+
services.TryAddTransient();
services.TryAddTransient(DbContextOptionsFactory.Create);
- var options = new AbpDbContextRegistrationOptions();
- optionsBuilder?.Invoke(options);
+ if (options.DefaultRepositoryDbContextType != typeof(TDbContext))
+ {
+ services.TryAddTransient(options.DefaultRepositoryDbContextType, typeof(TDbContext));
+ }
+
+ foreach (var dbContextType in options.ReplacedDbContextTypes)
+ {
+ services.Replace(ServiceDescriptor.Transient(dbContextType, typeof(TDbContext)));
+ }
new EfCoreRepositoryRegistrar(options)
- .AddRepositories(services, typeof(TDbContext));
+ .AddRepositories(services);
return services;
}
diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
index 8ed4a53b44..869d090176 100644
--- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
+++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
@@ -12,7 +12,7 @@ using Volo.Abp.Threading;
namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
{
public class EfCoreRepository : EfCoreRepository, IEfCoreRepository
- where TDbContext : AbpDbContext
+ where TDbContext : IEfCoreDbContext
where TEntity : class, IEntity
{
public EfCoreRepository(IDbContextProvider dbContextProvider)
@@ -25,12 +25,12 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
IEfCoreRepository,
ISupportsExplicitLoading
- where TDbContext : AbpDbContext
+ where TDbContext : IEfCoreDbContext
where TEntity : class, IEntity
{
public virtual DbSet DbSet => DbContext.Set();
- DbContext IEfCoreRepository.DbContext => DbContext;
+ DbContext IEfCoreRepository.DbContext => DbContext.As();
protected virtual TDbContext DbContext => _dbContextProvider.GetDbContext();
diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
index 07bc3f6760..4b2edcc6dc 100644
--- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
+++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
@@ -17,7 +17,7 @@ using Volo.Abp.Uow;
namespace Volo.Abp.EntityFrameworkCore
{
- public abstract class AbpDbContext : DbContext
+ public abstract class AbpDbContext : DbContext, IEfCoreDbContext
where TDbContext : DbContext
{
public Guid? CurrentTenantId => CurrentTenant?.Id;
diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextRegistrationOptions.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextRegistrationOptions.cs
index 172461a913..484ceaa3ba 100644
--- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextRegistrationOptions.cs
+++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextRegistrationOptions.cs
@@ -1,10 +1,14 @@
-using Volo.Abp.Data;
+using System;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.EntityFrameworkCore.DependencyInjection
{
public class AbpDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IAbpDbContextRegistrationOptionsBuilder
{
+ public AbpDbContextRegistrationOptions(Type originalDbContextType)
+ : base(originalDbContextType)
+ {
+ }
}
}
\ No newline at end of file
diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/IAbpDbContextRegistrationOptionsBuilder.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/IAbpDbContextRegistrationOptionsBuilder.cs
index db094128a6..0d812fb3cd 100644
--- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/IAbpDbContextRegistrationOptionsBuilder.cs
+++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/IAbpDbContextRegistrationOptionsBuilder.cs
@@ -1,10 +1,8 @@
-using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.EntityFrameworkCore.DependencyInjection
{
public interface IAbpDbContextRegistrationOptionsBuilder : ICommonDbContextRegistrationOptionsBuilder
{
-
}
}
\ No newline at end of file
diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IDbContextProvider.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IDbContextProvider.cs
index d975596df1..c4655fddd0 100644
--- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IDbContextProvider.cs
+++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IDbContextProvider.cs
@@ -1,7 +1,7 @@
namespace Volo.Abp.EntityFrameworkCore
{
public interface IDbContextProvider
- where TDbContext : AbpDbContext
+ where TDbContext : IEfCoreDbContext
{
TDbContext GetDbContext();
}
diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContext.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContext.cs
new file mode 100644
index 0000000000..7cba957724
--- /dev/null
+++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContext.cs
@@ -0,0 +1,86 @@
+using System;
+using System.Collections.Generic;
+using System.Threading;
+using System.Threading.Tasks;
+using JetBrains.Annotations;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.ChangeTracking;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Internal;
+
+namespace Volo.Abp.EntityFrameworkCore
+{
+ public interface IEfCoreDbContext : IDisposable, IInfrastructure, IDbContextDependencies, IDbSetCache, IDbContextPoolable
+ {
+ EntityEntry Attach([NotNull] TEntity entity) where TEntity : class;
+
+ EntityEntry Attach([NotNull] object entity);
+
+ int SaveChanges();
+
+ int SaveChanges(bool acceptAllChangesOnSuccess);
+
+ Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default);
+
+ Task SaveChangesAsync(CancellationToken cancellationToken = default);
+
+ DbSet Set()
+ where T: class;
+
+ DatabaseFacade Database { get; }
+
+ ChangeTracker ChangeTracker { get; }
+
+ EntityEntry Add([NotNull] object entity);
+
+ EntityEntry Add([NotNull] TEntity entity) where TEntity : class;
+
+ Task AddAsync([NotNull] object entity, CancellationToken cancellationToken = default);
+
+ Task> AddAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default) where TEntity : class;
+
+ void AddRange([NotNull] IEnumerable