Commit a2b235de authored by Savorboard's avatar Savorboard

fixed entityframework rename table name prefix bug. #84

parent a2524f12
...@@ -25,22 +25,32 @@ namespace DotNetCore.CAP ...@@ -25,22 +25,32 @@ namespace DotNetCore.CAP
services.AddScoped<ICallbackPublisher, CapPublisher>(); services.AddScoped<ICallbackPublisher, CapPublisher>();
services.AddTransient<IAdditionalProcessor, DefaultAdditionalProcessor>(); services.AddTransient<IAdditionalProcessor, DefaultAdditionalProcessor>();
AddSingletionMySqlOptions(services);
}
private void AddSingletionMySqlOptions(IServiceCollection services)
{
var mysqlOptions = new MySqlOptions(); var mysqlOptions = new MySqlOptions();
_configure(mysqlOptions); _configure(mysqlOptions);
if (mysqlOptions.DbContextType != null) if (mysqlOptions.DbContextType != null)
{
services.AddSingleton(x => services.AddSingleton(x =>
{ {
using (var scope = x.CreateScope()) using (var scope = x.CreateScope())
{ {
var provider = scope.ServiceProvider; var provider = scope.ServiceProvider;
var dbContext = (DbContext) provider.GetService(mysqlOptions.DbContextType); var dbContext = (DbContext)provider.GetService(mysqlOptions.DbContextType);
mysqlOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; mysqlOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString;
return mysqlOptions; return mysqlOptions;
} }
}); });
}
else else
{
services.AddSingleton(mysqlOptions); services.AddSingleton(mysqlOptions);
}
} }
} }
} }
\ No newline at end of file
...@@ -16,6 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -16,6 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection
{ {
if (configure == null) throw new ArgumentNullException(nameof(configure)); if (configure == null) throw new ArgumentNullException(nameof(configure));
options.RegisterExtension(new MySqlCapOptionsExtension(configure)); options.RegisterExtension(new MySqlCapOptionsExtension(configure));
return options; return options;
...@@ -32,10 +33,11 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -32,10 +33,11 @@ namespace Microsoft.Extensions.DependencyInjection
{ {
if (configure == null) throw new ArgumentNullException(nameof(configure)); if (configure == null) throw new ArgumentNullException(nameof(configure));
var efOptions = new EFOptions {DbContextType = typeof(TContext)}; options.RegisterExtension(new MySqlCapOptionsExtension(x =>
configure(efOptions); {
configure(x);
options.RegisterExtension(new MySqlCapOptionsExtension(configure)); x.DbContextType = typeof(TContext);
}));
return options; return options;
} }
......
...@@ -24,7 +24,7 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -24,7 +24,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static CapOptions UseEntityFramework<TContext>(this CapOptions options) public static CapOptions UseEntityFramework<TContext>(this CapOptions options)
where TContext : DbContext where TContext : DbContext
{ {
return options.UseEntityFramework<TContext>(opt => { opt.DbContextType = typeof(TContext); }); return options.UseEntityFramework<TContext>(opt => { });
} }
public static CapOptions UseEntityFramework<TContext>(this CapOptions options, Action<EFOptions> configure) public static CapOptions UseEntityFramework<TContext>(this CapOptions options, Action<EFOptions> configure)
...@@ -32,10 +32,11 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -32,10 +32,11 @@ namespace Microsoft.Extensions.DependencyInjection
{ {
if (configure == null) throw new ArgumentNullException(nameof(configure)); if (configure == null) throw new ArgumentNullException(nameof(configure));
var efOptions = new EFOptions {DbContextType = typeof(TContext)}; options.RegisterExtension(new PostgreSqlCapOptionsExtension(x =>
configure(efOptions); {
configure(x);
options.RegisterExtension(new PostgreSqlCapOptionsExtension(configure)); x.DbContextType = typeof(TContext);
}));
return options; return options;
} }
......
...@@ -25,10 +25,16 @@ namespace DotNetCore.CAP ...@@ -25,10 +25,16 @@ namespace DotNetCore.CAP
services.AddScoped<ICallbackPublisher, CapPublisher>(); services.AddScoped<ICallbackPublisher, CapPublisher>();
services.AddTransient<IAdditionalProcessor, DefaultAdditionalProcessor>(); services.AddTransient<IAdditionalProcessor, DefaultAdditionalProcessor>();
AddSingletonPostgreSqlOptions(services);
}
private void AddSingletonPostgreSqlOptions(IServiceCollection services)
{
var postgreSqlOptions = new PostgreSqlOptions(); var postgreSqlOptions = new PostgreSqlOptions();
_configure(postgreSqlOptions); _configure(postgreSqlOptions);
if (postgreSqlOptions.DbContextType != null) if (postgreSqlOptions.DbContextType != null)
{
services.AddSingleton(x => services.AddSingleton(x =>
{ {
using (var scope = x.CreateScope()) using (var scope = x.CreateScope())
...@@ -39,8 +45,11 @@ namespace DotNetCore.CAP ...@@ -39,8 +45,11 @@ namespace DotNetCore.CAP
return postgreSqlOptions; return postgreSqlOptions;
} }
}); });
}
else else
{
services.AddSingleton(postgreSqlOptions); services.AddSingleton(postgreSqlOptions);
}
} }
} }
} }
\ No newline at end of file
...@@ -24,7 +24,7 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -24,7 +24,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static CapOptions UseEntityFramework<TContext>(this CapOptions options) public static CapOptions UseEntityFramework<TContext>(this CapOptions options)
where TContext : DbContext where TContext : DbContext
{ {
return options.UseEntityFramework<TContext>(opt => { opt.DbContextType = typeof(TContext); }); return options.UseEntityFramework<TContext>(opt => { });
} }
public static CapOptions UseEntityFramework<TContext>(this CapOptions options, Action<EFOptions> configure) public static CapOptions UseEntityFramework<TContext>(this CapOptions options, Action<EFOptions> configure)
...@@ -32,10 +32,11 @@ namespace Microsoft.Extensions.DependencyInjection ...@@ -32,10 +32,11 @@ namespace Microsoft.Extensions.DependencyInjection
{ {
if (configure == null) throw new ArgumentNullException(nameof(configure)); if (configure == null) throw new ArgumentNullException(nameof(configure));
var efOptions = new EFOptions {DbContextType = typeof(TContext)}; options.RegisterExtension(new SqlServerCapOptionsExtension(x =>
configure(efOptions); {
configure(x);
options.RegisterExtension(new SqlServerCapOptionsExtension(configure)); x.DbContextType = typeof(TContext);
}));
return options; return options;
} }
......
...@@ -24,6 +24,7 @@ namespace DotNetCore.CAP ...@@ -24,6 +24,7 @@ namespace DotNetCore.CAP
services.AddScoped<ICapPublisher, CapPublisher>(); services.AddScoped<ICapPublisher, CapPublisher>();
services.AddScoped<ICallbackPublisher, CapPublisher>(); services.AddScoped<ICallbackPublisher, CapPublisher>();
services.AddTransient<IAdditionalProcessor, DefaultAdditionalProcessor>(); services.AddTransient<IAdditionalProcessor, DefaultAdditionalProcessor>();
AddSqlServerOptions(services); AddSqlServerOptions(services);
} }
...@@ -34,18 +35,22 @@ namespace DotNetCore.CAP ...@@ -34,18 +35,22 @@ namespace DotNetCore.CAP
_configure(sqlServerOptions); _configure(sqlServerOptions);
if (sqlServerOptions.DbContextType != null) if (sqlServerOptions.DbContextType != null)
{
services.AddSingleton(x => services.AddSingleton(x =>
{ {
using (var scope = x.CreateScope()) using (var scope = x.CreateScope())
{ {
var provider = scope.ServiceProvider; var provider = scope.ServiceProvider;
var dbContext = (DbContext) provider.GetService(sqlServerOptions.DbContextType); var dbContext = (DbContext)provider.GetService(sqlServerOptions.DbContextType);
sqlServerOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; sqlServerOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString;
return sqlServerOptions; return sqlServerOptions;
} }
}); });
}
else else
{
services.AddSingleton(sqlServerOptions); services.AddSingleton(sqlServerOptions);
}
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment