- ABP Framework version: v8.2.2
- UI Type: MVC
- Database System: EF Core (SQL Server,MySQL)
- Tiered (for MVC) or Auth Server Separated (for Angular): yes
- Exception message and full stack trace:
- Steps to reproduce the issue:
I created a app-pro template project named as MyApp. then created a module template project named as MyModule.
The MyApp use SQLServer, but i need use MySQL at MyModule, how to implement it?
I added the Microsoft.EntityFrameworkCore.Design and Volo.Abp.EntityFrameworkCore.MySQL to the MyModule.EntityFrameworkCore.csproj, and added a DbContextFactory class.
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
namespace MyModule.EntityFrameworkCore;
public class MyModuleDbContextFactory : IDesignTimeDbContextFactory<MyModuleDbContext>
{
public MyModuleDbContext CreateDbContext(string[] args)
{
var configuration = BuildConfiguration();
var builder = new DbContextOptionsBuilder<MyModuleDbContext>()
.UseMySql(configuration.GetConnectionString(MyModuleDbProperties.ConnectionStringName), MySqlServerVersion.LatestSupportedServerVersion);
return new MyModuleDbContext(builder.Options);
}
private static IConfigurationRoot BuildConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false);
return builder.Build();
}
}
Migrations files generated success by use dotnet ef migrations add Initial
.
But how to configure the MyModule.EntityFrameworkCore to use MySQL?
9 Answer(s)
-
0
hi
The MyApp use SQLServer, but i need use MySQL at MyModule, how to implement it?
The module does not depend on any database provider, it uses EF Core.
And your
MyApp
will decide which database provider will be used. -
0
Is there a way to let MyModule will decide which database provider will be used?
I notice the the EntityFrameworkCoreModule of MyApp can use
options.UseMySQL();
to change the DBMS.public class MyAppEntityFrameworkCoreModule : AbpModule { public override void PreConfigureServices(ServiceConfigurationContext context) { BaaSoEfCoreEntityExtensionMappings.Configure(); } public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAbpDbContext<BaaSoDbContext>(options => { options.AddDefaultRepositories(includeAllEntities: true); }); context.Services.AddAbpDbContext<BaaSoTenantDbContext>(options => { options.AddDefaultRepositories(includeAllEntities: true); }); Configure<AbpDbContextOptions>(options => { /* The main point to change your DBMS. * See also BaaSoDbContextFactoryBase for EF Core tooling. */ options.UseMySQL(); }); } }
-
0
The module template is designed to be reused in every type of app.
EF Core or Mongodb. Monolithic or microservice.
So it can't decide what
database provider
should used. -
0
Thanks , I understand it.
But I would like to know some details.
Base the EntityFrameworkCoreModule of module template, add DbContextFactory to configure the DBMS. but this class is only valid for EntityFrameworkCore tool?
Real database provided by App at runtime? So if MyApp use SQLServer, but MyModule migrated by MySQL, will throw exception?
-
0
hi
You shouldn't add
DbContextFactory
andmigrations
for the module template project(MyModule
)Like I said. The module has nothing to do with the database, so it doesn't need the above.
-
0
I know the best way is reference modules in application project and migrations all database. This is in line with DDD's architectural design. But my platform needs loads modules as plugin, so i must migrate the database in module. i modify the module template, and resolved most issue, now it works fine.
I know shouldn't add DbContextFactory and migrations for the EntityFrameworkCoreModule, but there is no better option at the moment.I just want to know some technical details to redesign later.
Get down to business, Is AbpDbContextOptions a singleton for the entire life of the runtime?
-
0
hi
AbpDbContextOptions
is standardIOptions<>
, and itssingleton
by default.https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-8.0
https://github.com/dotnet/runtime/blob/release/8.0/src/libraries/Microsoft.Extensions.Options/src/OptionsServiceCollectionExtensions.cs#L26
-
0
Thanks ~~
-
0
: )