Browse Source

Implemented IEfCoreDbContext everywhere.

pull/189/head
Halil İbrahim Kalkan 8 years ago
parent
commit
a5000e99a6
  1. 1
      src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk.EntityFrameworkCore.csproj
  2. 10
      src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbContext.cs
  3. 7
      src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskEntityFrameworkCoreModule.cs
  4. 92
      src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/20180105184326_MultiTenancyModuleAdded.Designer.cs
  5. 63
      src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/20180105184326_MultiTenancyModuleAdded.cs
  6. 58
      src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/AbpDeskDbContextModelSnapshot.cs
  7. 4
      src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs
  8. 6
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
  9. 1
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextRegistrationOptions.cs
  10. 2
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IDbContextProvider.cs
  11. 76
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContext.cs
  12. 2
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/EfCoreDatabaseApi.cs
  13. 11
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/EfCoreTransactionApi.cs
  14. 4
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/UnitOfWorkDbContextProvider.cs
  15. 5
      src/Volo.Abp.MultiTenancy.EntityFrameworkCore/Volo/Abp/MultiTenancy/EntityFrameworkCore/AbpMultiTenancyEntityFrameworkCoreModule.cs

1
src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk.EntityFrameworkCore.csproj

@ -12,6 +12,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Volo.Abp.MultiTenancy.EntityFrameworkCore\Volo.Abp.MultiTenancy.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\AbpDesk.Domain\AbpDesk.Domain.csproj" />
<ProjectReference Include="..\..\Volo.Abp.EntityFrameworkCore\Volo.Abp.EntityFrameworkCore.csproj" />
</ItemGroup>

10
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<AbpDeskDbContext>
public class AbpDeskDbContext : AbpDbContext<AbpDeskDbContext>, IMultiTenancyDbContext
{
public DbSet<Ticket> Tickets { get; set; }
public DbSet<Tenant> Tenants { get; set; }
public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }
public AbpDeskDbContext(DbContextOptions<AbpDeskDbContext> 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<Ticket>(b =>
{

7
src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskEntityFrameworkCoreModule.cs

@ -1,10 +1,15 @@
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)

92
src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/20180105184326_MultiTenancyModuleAdded.Designer.cs

@ -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
}
}
}

63
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<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");
}
}
}

58
src/AbpDesk/AbpDesk.EntityFrameworkCore/Migrations/AbpDeskDbContextModelSnapshot.cs

@ -1,6 +1,12 @@
using Microsoft.EntityFrameworkCore;
// <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
{
@ -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<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
}
}
}

4
src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs

@ -7,7 +7,9 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class AbpEfCoreServiceCollectionExtensions
{
public static IServiceCollection AddAbpDbContext<TDbContext>(this IServiceCollection services, Action<IAbpDbContextRegistrationOptionsBuilder> optionsBuilder = null) //Created overload instead of default parameter
public static IServiceCollection AddAbpDbContext<TDbContext>(
this IServiceCollection services,
Action<IAbpDbContextRegistrationOptionsBuilder> optionsBuilder = null)
where TDbContext : AbpDbContext<TDbContext>
{
services.AddMemoryCache();

6
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<TDbContext, TEntity> : EfCoreRepository<TDbContext, TEntity, Guid>, IEfCoreRepository<TEntity>
where TDbContext : AbpDbContext<TDbContext>
where TDbContext : IEfCoreDbContext
where TEntity : class, IEntity<Guid>
{
public EfCoreRepository(IDbContextProvider<TDbContext> dbContextProvider)
@ -25,12 +25,12 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
IEfCoreRepository<TEntity, TPrimaryKey>,
ISupportsExplicitLoading<TEntity, TPrimaryKey>
where TDbContext : AbpDbContext<TDbContext>
where TDbContext : IEfCoreDbContext
where TEntity : class, IEntity<TPrimaryKey>
{
public virtual DbSet<TEntity> DbSet => DbContext.Set<TEntity>();
DbContext IEfCoreRepository<TEntity, TPrimaryKey>.DbContext => DbContext;
DbContext IEfCoreRepository<TEntity, TPrimaryKey>.DbContext => DbContext.As<DbContext>();
protected virtual TDbContext DbContext => _dbContextProvider.GetDbContext();

1
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextRegistrationOptions.cs

@ -1,4 +1,3 @@
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.EntityFrameworkCore.DependencyInjection

2
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IDbContextProvider.cs

@ -1,7 +1,7 @@
namespace Volo.Abp.EntityFrameworkCore
{
public interface IDbContextProvider<out TDbContext>
where TDbContext : AbpDbContext<TDbContext>
where TDbContext : IEfCoreDbContext
{
TDbContext GetDbContext();
}

76
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContext.cs

@ -1,14 +1,86 @@
using Microsoft.EntityFrameworkCore;
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
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);
}
}

2
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/EfCoreDatabaseApi.cs

@ -5,7 +5,7 @@ using Volo.Abp.EntityFrameworkCore;
namespace Volo.Abp.Uow.EntityFrameworkCore
{
public class EfCoreDatabaseApi<TDbContext> : IDatabaseApi, ISupportsSavingChanges
where TDbContext : AbpDbContext<TDbContext>
where TDbContext : IEfCoreDbContext
{
public TDbContext DbContext { get; }

11
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/EfCoreTransactionApi.cs

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@ -10,14 +11,14 @@ namespace Volo.Abp.Uow.EntityFrameworkCore
public class EfCoreTransactionApi : ITransactionApi, ISupportsRollback
{
public IDbContextTransaction DbContextTransaction { get; }
public DbContext StarterDbContext { get; }
public List<DbContext> AttendedDbContexts { get; }
public IEfCoreDbContext StarterDbContext { get; }
public List<IEfCoreDbContext> AttendedDbContexts { get; }
public EfCoreTransactionApi(IDbContextTransaction dbContextTransaction, DbContext starterDbContext)
public EfCoreTransactionApi(IDbContextTransaction dbContextTransaction, IEfCoreDbContext starterDbContext)
{
DbContextTransaction = dbContextTransaction;
StarterDbContext = starterDbContext;
AttendedDbContexts = new List<DbContext>();
AttendedDbContexts = new List<IEfCoreDbContext>();
}
public void Commit()
@ -26,7 +27,7 @@ namespace Volo.Abp.Uow.EntityFrameworkCore
foreach (var dbContext in AttendedDbContexts)
{
if (dbContext.HasRelationalTransactionManager())
if (dbContext.As<DbContext>().HasRelationalTransactionManager())
{
continue; //Relational databases use the shared transaction
}

4
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/UnitOfWorkDbContextProvider.cs

@ -11,7 +11,7 @@ namespace Volo.Abp.Uow.EntityFrameworkCore
//TODO: Implement logic in DefaultDbContextResolver.Resolve in old ABP.
public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbContext>
where TDbContext : AbpDbContext<TDbContext>
where TDbContext : IEfCoreDbContext
{
private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IConnectionStringResolver _connectionStringResolver;
@ -100,7 +100,7 @@ namespace Volo.Abp.Uow.EntityFrameworkCore
var dbContext = unitOfWork.ServiceProvider.GetRequiredService<TDbContext>();
if (dbContext.HasRelationalTransactionManager())
if (dbContext.As<DbContext>().HasRelationalTransactionManager())
{
dbContext.Database.UseTransaction(activeTransaction.DbContextTransaction.GetDbTransaction());
}

5
src/Volo.Abp.MultiTenancy.EntityFrameworkCore/Volo/Abp/MultiTenancy/EntityFrameworkCore/AbpMultiTenancyEntityFrameworkCoreModule.cs

@ -10,6 +10,11 @@ namespace Volo.Abp.MultiTenancy.EntityFrameworkCore
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddAbpDbContext<MultiTenancyDbContext>(options =>
{
options.WithDefaultRepositories();
});
services.AddAssemblyOf<AbpMultiTenancyEntityFrameworkCoreModule>();
}
}

Loading…
Cancel
Save