Skip to content

Commit c2ffd76

Browse files
committed
Added a second db.
1 parent 9a67a39 commit c2ffd76

14 files changed

+3218
-363
lines changed

EfCoreMigrationDemo/src/BookStore.DbMigrator/appsettings.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"ConnectionStrings": {
3-
"Default": "Server=(LocalDb)\\MSSQLLocalDB;Database=BookStore;Trusted_Connection=True"
3+
"Default": "Server=(LocalDb)\\MSSQLLocalDB;Database=BookStore;Trusted_Connection=True",
4+
"AbpPermissionManagement": "Server=(LocalDb)\\MSSQLLocalDB;Database=BookStore_SecondDb;Trusted_Connection=True",
5+
"AbpSettingManagement": "Server=(LocalDb)\\MSSQLLocalDB;Database=BookStore_SecondDb;Trusted_Connection=True",
6+
"AbpAuditLogging": "Server=(LocalDb)\\MSSQLLocalDB;Database=BookStore_SecondDb;Trusted_Connection=True"
47
},
58
"IdentityServer": {
69
"Clients": {

EfCoreMigrationDemo/src/BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,20 @@ protected override void OnModelCreating(ModelBuilder builder)
6464

6565
/* Include modules to your migration db context */
6666

67-
builder.ConfigurePermissionManagement();
68-
builder.ConfigureSettingManagement();
6967
builder.ConfigureBackgroundJobs();
70-
builder.ConfigureAuditLogging();
7168
builder.ConfigureIdentity();
7269
builder.ConfigureIdentityServer();
7370
builder.ConfigureFeatureManagement();
7471
builder.ConfigureTenantManagement();
7572

7673
/* Configure your own tables/entities inside here */
7774

78-
//builder.Entity<YourEntity>(b =>
79-
//{
80-
// b.ToTable(BookStoreConsts.DbTablePrefix + "YourEntities", BookStoreConsts.DbSchema);
81-
// b.ConfigureByConvention(); //auto configure for the base class props
82-
// //...
83-
//});
75+
//builder.Entity<YourEntity>(b =>
76+
//{
77+
// b.ToTable(BookStoreConsts.DbTablePrefix + "YourEntities", BookStoreConsts.DbSchema);
78+
// b.ConfigureByConvention(); //auto configure for the base class props
79+
// //...
80+
//});
8481
}
8582
}
8683
}

EfCoreMigrationDemo/src/BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public override void ConfigureServices(ServiceConfigurationContext context)
4141
options.AddDefaultRepositories(includeAllEntities: true);
4242
});
4343

44+
context.Services.AddAbpDbContext<BookStoreSecondDbContext>();
45+
4446
Configure<AbpDbContextOptions>(options =>
4547
{
4648
/* The main point to change your DBMS.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Volo.Abp.AuditLogging.EntityFrameworkCore;
3+
using Volo.Abp.Data;
4+
using Volo.Abp.EntityFrameworkCore;
5+
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
6+
using Volo.Abp.SettingManagement.EntityFrameworkCore;
7+
8+
namespace BookStore.EntityFrameworkCore
9+
{
10+
[ConnectionStringName("AbpPermissionManagement")]
11+
public class BookStoreSecondDbContext :
12+
AbpDbContext<BookStoreSecondDbContext>
13+
{
14+
public BookStoreSecondDbContext(
15+
DbContextOptions<BookStoreSecondDbContext> options)
16+
: base(options)
17+
{
18+
}
19+
20+
protected override void OnModelCreating(ModelBuilder builder)
21+
{
22+
base.OnModelCreating(builder);
23+
24+
/* Include modules to your migration db context */
25+
builder.ConfigurePermissionManagement();
26+
builder.ConfigureSettingManagement();
27+
builder.ConfigureAuditLogging();
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.IO;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Design;
4+
using Microsoft.Extensions.Configuration;
5+
6+
namespace BookStore.EntityFrameworkCore
7+
{
8+
/* This class is needed for EF Core console commands
9+
* (like Add-Migration and Update-Database commands) */
10+
public class BookStoreSecondDbContextFactory
11+
: IDesignTimeDbContextFactory<BookStoreSecondDbContext>
12+
{
13+
public BookStoreSecondDbContext CreateDbContext(string[] args)
14+
{
15+
var configuration = BuildConfiguration();
16+
var builder = new DbContextOptionsBuilder<BookStoreSecondDbContext>()
17+
.UseSqlServer(configuration.GetConnectionString("AbpPermissionManagement"));
18+
return new BookStoreSecondDbContext(builder.Options);
19+
}
20+
21+
private static IConfigurationRoot BuildConfiguration()
22+
{
23+
var builder = new ConfigurationBuilder()
24+
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../BookStore.DbMigrator/"))
25+
.AddJsonFile("appsettings.json", optional: false);
26+
27+
return builder.Build();
28+
}
29+
}
30+
}

EfCoreMigrationDemo/src/BookStore.EntityFrameworkCore/EntityFrameworkCore/EntityFrameworkCoreBookStoreDbSchemaMigrator.cs

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ await _serviceProvider
3030
.GetRequiredService<BookStoreDbContext>()
3131
.Database
3232
.MigrateAsync();
33+
34+
await _serviceProvider
35+
.GetRequiredService<BookStoreSecondDbContext>()
36+
.Database
37+
.MigrateAsync();
3338
}
3439
}
3540
}

0 commit comments

Comments
 (0)