mirror of https://github.com/abpframework/abp.git
committed by
GitHub
43 changed files with 640 additions and 108 deletions
@ -0,0 +1,92 @@ |
|||||
|
// <auto-generated />
|
||||
|
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<int>("Id") |
||||
|
.ValueGeneratedOnAdd(); |
||||
|
|
||||
|
b.Property<string>("Body") |
||||
|
.HasMaxLength(65536); |
||||
|
|
||||
|
b.Property<string>("ConcurrencyStamp") |
||||
|
.IsConcurrencyToken(); |
||||
|
|
||||
|
b.Property<string>("Title") |
||||
|
.IsRequired() |
||||
|
.HasMaxLength(256); |
||||
|
|
||||
|
b.HasKey("Id"); |
||||
|
|
||||
|
b.ToTable("DskTickets"); |
||||
|
}); |
||||
|
|
||||
|
modelBuilder.Entity("Volo.Abp.MultiTenancy.Tenant", b => |
||||
|
{ |
||||
|
b.Property<Guid>("Id") |
||||
|
.ValueGeneratedOnAdd(); |
||||
|
|
||||
|
b.Property<string>("Name") |
||||
|
.IsRequired() |
||||
|
.HasMaxLength(64); |
||||
|
|
||||
|
b.HasKey("Id"); |
||||
|
|
||||
|
b.HasIndex("Name"); |
||||
|
|
||||
|
b.ToTable("MtTenants"); |
||||
|
}); |
||||
|
|
||||
|
modelBuilder.Entity("Volo.Abp.MultiTenancy.TenantConnectionString", b => |
||||
|
{ |
||||
|
b.Property<Guid>("Id") |
||||
|
.ValueGeneratedOnAdd(); |
||||
|
|
||||
|
b.Property<string>("Name") |
||||
|
.IsRequired() |
||||
|
.HasMaxLength(256); |
||||
|
|
||||
|
b.Property<Guid>("TenantId"); |
||||
|
|
||||
|
b.Property<string>("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
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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<Guid>(type: "uniqueidentifier", nullable: false), |
||||
|
Name = table.Column<string>(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<Guid>(type: "uniqueidentifier", nullable: false), |
||||
|
Name = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false), |
||||
|
TenantId = table.Column<Guid>(type: "uniqueidentifier", nullable: false), |
||||
|
Value = table.Column<string>(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"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -1,10 +1,14 @@ |
|||||
using Volo.Abp.Data; |
using System; |
||||
using Volo.Abp.DependencyInjection; |
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
namespace Volo.Abp.EntityFrameworkCore.DependencyInjection |
namespace Volo.Abp.EntityFrameworkCore.DependencyInjection |
||||
{ |
{ |
||||
public class AbpDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IAbpDbContextRegistrationOptionsBuilder |
public class AbpDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IAbpDbContextRegistrationOptionsBuilder |
||||
{ |
{ |
||||
|
public AbpDbContextRegistrationOptions(Type originalDbContextType) |
||||
|
: base(originalDbContextType) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
@ -1,10 +1,8 @@ |
|||||
using Volo.Abp.Data; |
|
||||
using Volo.Abp.DependencyInjection; |
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
namespace Volo.Abp.EntityFrameworkCore.DependencyInjection |
namespace Volo.Abp.EntityFrameworkCore.DependencyInjection |
||||
{ |
{ |
||||
public interface IAbpDbContextRegistrationOptionsBuilder : ICommonDbContextRegistrationOptionsBuilder |
public interface IAbpDbContextRegistrationOptionsBuilder : ICommonDbContextRegistrationOptionsBuilder |
||||
{ |
{ |
||||
|
|
||||
} |
} |
||||
} |
} |
||||
@ -1,7 +1,7 @@ |
|||||
namespace Volo.Abp.EntityFrameworkCore |
namespace Volo.Abp.EntityFrameworkCore |
||||
{ |
{ |
||||
public interface IDbContextProvider<out TDbContext> |
public interface IDbContextProvider<out TDbContext> |
||||
where TDbContext : AbpDbContext<TDbContext> |
where TDbContext : IEfCoreDbContext |
||||
{ |
{ |
||||
TDbContext GetDbContext(); |
TDbContext GetDbContext(); |
||||
} |
} |
||||
|
|||||
@ -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<IServiceProvider>, IDbContextDependencies, IDbSetCache, IDbContextPoolable |
||||
|
{ |
||||
|
EntityEntry<TEntity> Attach<TEntity>([NotNull] TEntity entity) where TEntity : class; |
||||
|
|
||||
|
EntityEntry Attach([NotNull] object entity); |
||||
|
|
||||
|
int SaveChanges(); |
||||
|
|
||||
|
int SaveChanges(bool acceptAllChangesOnSuccess); |
||||
|
|
||||
|
Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default); |
||||
|
|
||||
|
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default); |
||||
|
|
||||
|
DbSet<T> Set<T>() |
||||
|
where T: class; |
||||
|
|
||||
|
DatabaseFacade Database { get; } |
||||
|
|
||||
|
ChangeTracker ChangeTracker { get; } |
||||
|
|
||||
|
EntityEntry Add([NotNull] object entity); |
||||
|
|
||||
|
EntityEntry<TEntity> Add<TEntity>([NotNull] TEntity entity) where TEntity : class; |
||||
|
|
||||
|
Task<EntityEntry> AddAsync([NotNull] object entity, CancellationToken cancellationToken = default); |
||||
|
|
||||
|
Task<EntityEntry<TEntity>> AddAsync<TEntity>([NotNull] TEntity entity, CancellationToken cancellationToken = default) where TEntity : class; |
||||
|
|
||||
|
void AddRange([NotNull] IEnumerable<object> entities); |
||||
|
|
||||
|
void AddRange([NotNull] params object[] entities); |
||||
|
|
||||
|
Task AddRangeAsync([NotNull] params object[] entities); |
||||
|
|
||||
|
Task AddRangeAsync([NotNull] IEnumerable<object> entities, CancellationToken cancellationToken = default); |
||||
|
|
||||
|
void AttachRange([NotNull] IEnumerable<object> entities); |
||||
|
|
||||
|
void AttachRange([NotNull] params object[] entities); |
||||
|
|
||||
|
EntityEntry<TEntity> Entry<TEntity>([NotNull] TEntity entity) where TEntity : class; |
||||
|
|
||||
|
EntityEntry Entry([NotNull] object entity); |
||||
|
|
||||
|
object Find([NotNull] Type entityType, [NotNull] params object[] keyValues); |
||||
|
|
||||
|
TEntity Find<TEntity>([NotNull] params object[] keyValues) where TEntity : class; |
||||
|
|
||||
|
Task<object> FindAsync([NotNull] Type entityType, [NotNull] object[] keyValues, CancellationToken cancellationToken); |
||||
|
|
||||
|
Task<TEntity> FindAsync<TEntity>([NotNull] object[] keyValues, CancellationToken cancellationToken) where TEntity : class; |
||||
|
|
||||
|
Task<TEntity> FindAsync<TEntity>([NotNull] params object[] keyValues) where TEntity : class; |
||||
|
|
||||
|
Task<object> FindAsync([NotNull] Type entityType, [NotNull] params object[] keyValues); |
||||
|
|
||||
|
EntityEntry<TEntity> Remove<TEntity>([NotNull] TEntity entity) where TEntity : class; |
||||
|
|
||||
|
EntityEntry Remove([NotNull] object entity); |
||||
|
|
||||
|
void RemoveRange([NotNull] IEnumerable<object> entities); |
||||
|
|
||||
|
void RemoveRange([NotNull] params object[] entities); |
||||
|
|
||||
|
EntityEntry<TEntity> Update<TEntity>([NotNull] TEntity entity) where TEntity : class; |
||||
|
|
||||
|
EntityEntry Update([NotNull] object entity); |
||||
|
|
||||
|
void UpdateRange([NotNull] params object[] entities); |
||||
|
|
||||
|
void UpdateRange([NotNull] IEnumerable<object> entities); |
||||
|
} |
||||
|
} |
||||
@ -1,10 +1,13 @@ |
|||||
using Volo.Abp.Data; |
using System; |
||||
using Volo.Abp.DependencyInjection; |
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
namespace Volo.Abp.MemoryDb.DependencyInjection |
namespace Volo.Abp.MemoryDb.DependencyInjection |
||||
{ |
{ |
||||
public class MemoryDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IMemoryDbContextRegistrationOptionsBuilder |
public class MemoryDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IMemoryDbContextRegistrationOptionsBuilder |
||||
{ |
{ |
||||
|
public MemoryDbContextRegistrationOptions(Type originalDbContextType) |
||||
|
: base(originalDbContextType) |
||||
|
{ |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
@ -1,10 +1,13 @@ |
|||||
using Volo.Abp.Data; |
using System; |
||||
using Volo.Abp.DependencyInjection; |
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
namespace Volo.Abp.MongoDB.DependencyInjection |
namespace Volo.Abp.MongoDB.DependencyInjection |
||||
{ |
{ |
||||
public class MongoDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IMongoDbContextRegistrationOptionsBuilder |
public class MongoDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IMongoDbContextRegistrationOptionsBuilder |
||||
{ |
{ |
||||
|
public MongoDbContextRegistrationOptions(Type originalDbContextType) |
||||
|
: base(originalDbContextType) |
||||
|
{ |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
@ -1,9 +1,9 @@ |
|||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||
using Microsoft.Extensions.Options; |
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.Data; |
||||
using Volo.Abp.DependencyInjection; |
using Volo.Abp.DependencyInjection; |
||||
using Volo.Abp.MultiTenancy; |
|
||||
|
|
||||
namespace Volo.Abp.Data.MultiTenancy |
namespace Volo.Abp.MultiTenancy |
||||
{ |
{ |
||||
[Dependency(ReplaceServices = true)] |
[Dependency(ReplaceServices = true)] |
||||
public class MultiTenantConnectionStringResolver : DefaultConnectionStringResolver |
public class MultiTenantConnectionStringResolver : DefaultConnectionStringResolver |
||||
@ -0,0 +1,12 @@ |
|||||
|
using Microsoft.EntityFrameworkCore; |
||||
|
using Volo.Abp.EntityFrameworkCore; |
||||
|
|
||||
|
namespace Volo.Abp.MultiTenancy.EntityFrameworkCore |
||||
|
{ |
||||
|
public interface IMultiTenancyDbContext : IEfCoreDbContext |
||||
|
{ |
||||
|
DbSet<Tenant> Tenants { get; set; } |
||||
|
|
||||
|
DbSet<TenantConnectionString> TenantConnectionStrings { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
using Microsoft.EntityFrameworkCore; |
||||
|
|
||||
|
namespace Volo.Abp.MultiTenancy.EntityFrameworkCore |
||||
|
{ |
||||
|
public static class MultiTenancyDbContextModelCreatingExtensions |
||||
|
{ |
||||
|
public static void ConfigureAbpMultiTenancy(this IMultiTenancyDbContext dbContext, ModelBuilder builder) |
||||
|
{ |
||||
|
builder.Entity<Tenant>(b => |
||||
|
{ |
||||
|
b.ToTable("MtTenants"); //TODO: Make all table and schema names changeable
|
||||
|
|
||||
|
b.Property(t => t.Name).IsRequired().HasMaxLength(TenantConsts.MaxNameLength); |
||||
|
|
||||
|
b.HasMany(u => u.ConnectionStrings).WithOne().HasForeignKey(uc => uc.TenantId).IsRequired(); |
||||
|
|
||||
|
b.HasIndex(u => u.Name); |
||||
|
}); |
||||
|
|
||||
|
builder.Entity<TenantConnectionString>(b => |
||||
|
{ |
||||
|
b.ToTable("MtTenantConnectionStrings"); |
||||
|
|
||||
|
b.Property(cs => cs.Name).IsRequired().HasMaxLength(TenantConnectionStringConsts.MaxNameLength); |
||||
|
b.Property(cs => cs.Value).IsRequired().HasMaxLength(TenantConnectionStringConsts.MaxValueLength); |
||||
|
|
||||
|
b.HasIndex(cs => cs.TenantId); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using Microsoft.EntityFrameworkCore; |
||||
|
|
||||
|
namespace Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext |
||||
|
{ |
||||
|
public interface IThirdDbContext : IEfCoreDbContext |
||||
|
{ |
||||
|
DbSet<ThirdDbContextDummyEntity> DummyEntities { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
using Microsoft.EntityFrameworkCore; |
||||
|
|
||||
|
namespace Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext |
||||
|
{ |
||||
|
/* This dbcontext is just for testing to replace dbcontext from the application using AbpDbContextRegistrationOptions.ReplaceDbContext |
||||
|
*/ |
||||
|
public class ThirdDbContext : AbpDbContext<ThirdDbContext>, IThirdDbContext |
||||
|
{ |
||||
|
public DbSet<ThirdDbContextDummyEntity> DummyEntities { get; set; } |
||||
|
|
||||
|
public ThirdDbContext(DbContextOptions<ThirdDbContext> options) |
||||
|
: base(options) |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using Volo.Abp.Domain.Entities; |
||||
|
|
||||
|
namespace Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext |
||||
|
{ |
||||
|
public class ThirdDbContextDummyEntity : AggregateRoot |
||||
|
{ |
||||
|
public string Value { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Domain.Repositories; |
||||
|
using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext; |
||||
|
using Volo.Abp.TestApp.EntityFrameworkCore; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.EntityFrameworkCore |
||||
|
{ |
||||
|
public class DbContext_Replace_Tests : EntityFrameworkCoreTestBase |
||||
|
{ |
||||
|
private readonly IRepository<ThirdDbContextDummyEntity> _dummyRepository; |
||||
|
|
||||
|
public DbContext_Replace_Tests() |
||||
|
{ |
||||
|
_dummyRepository = ServiceProvider.GetRequiredService<IRepository<ThirdDbContextDummyEntity>>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Replace_DbContext() |
||||
|
{ |
||||
|
(ServiceProvider.GetRequiredService<IThirdDbContext>() is TestAppDbContext).ShouldBeTrue(); |
||||
|
|
||||
|
(_dummyRepository.GetDbContext() is IThirdDbContext).ShouldBeTrue(); |
||||
|
(_dummyRepository.GetDbContext() is TestAppDbContext).ShouldBeTrue(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue