中 | EN
Example:
Install-Package Masa.Contrib.Isolation.UoW.EF
Install-Package Masa.Contrib.Isolation.MultiEnvironment
Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer
- 配置appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;uid=sa;pwd=P@ssw0rd;database=identity;",
"Isolations": [
{
"Environment": "development",
"ConnectionString": "server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity;"
},
{
"Environment": "staging",
"ConnectionString": "server=localhost,1672;uid=sa;pwd=P@ssw0rd;database=identity;"
}
]
}
}
- 1.1 When the current environment is development: database address: server=localhost,1674;uid=sa;pwd=P@ssw0rd;database=identity;
- 1.2 When the current environment is staging: database address: server=localhost,1672;uid=sa;pwd=P@ssw0rd;database=identity;
- 1.3 When the current environment is another environment: database address: server=localhost;uid=sa;pwd=P@ssw0rd;database=identity;
- Using Isolation.UoW.EF
builder.Services.AddEventBus(eventBusBuilder =>
{
eventBusBuilder.UseIsolationUoW<CustomDbContext>(
isolationBuilder => isolationBuilder.UseMultiEnvironment(),
dbOptions => dbOptions.UseSqlServer());
});
- DbContext needs to inherit IsolationDbContext
public class CustomDbContext : IsolationDbContext
{
public CustomDbContext(MasaDbContextOptions<CustomDbContext> options) : base(options)
{
}
}
- The class corresponding to the isolated table needs to implement IMultiEnvironment
You can also choose not to implement IMultiEnvironment when using physical isolation
- How is the environment resolved in the controller or MinimalAPI?
- The environment provides 7 parsers by default, and the execution order is: HttpContextItemParserProvider、 QueryStringParserProvider、 FormParserProvider、 RouteParserProvider、 HeaderParserProvider、 CookieParserProvider、 EnvironmentVariablesParserProvider (Get the parameters in the system environment variables, the parameters of the default environment isolation: ASPNETCORE_ENVIRONMENT)
- HttpContextItemParserProvider: Get tenant information through the Items property of the requested HttpContext
- QueryStringParserProvider: Get environment information through the requested QueryString
- https://github.com/masastack?ASPNETCORE_ENVIRONMENT=development (environment information is development)
- FormParserProvider: Get environment information through Form form
- RouteParserProvider: Get environment information through routing
- HeaderParserProvider: Get environment information through request headers
- CookieParserProvider: Get environmental information through cookies
- EnvironmentVariablesParserProvider: Get environment information through system environment variables
- The environment provides 7 parsers by default, and the execution order is: HttpContextItemParserProvider、 QueryStringParserProvider、 FormParserProvider、 RouteParserProvider、 HeaderParserProvider、 CookieParserProvider、 EnvironmentVariablesParserProvider (Get the parameters in the system environment variables, the parameters of the default environment isolation: ASPNETCORE_ENVIRONMENT)
- If the parser fails to resolve the environment, what is the last database used?
- If the parsing environment fails, return DefaultConnection directly
- How to change the default environment parameter name
builder.Services.AddEventBus(eventBusBuilder =>
{
eventBusBuilder.UseIsolationUoW<CustomDbContext>(
isolationBuilder => isolationBuilder.UseMultiEnvironment("env"),// Use environment isolation
dbOptions => dbOptions.UseSqlServer());
});
- How to change the parser
builder.Services.AddEventBus(eventBusBuilder =>
{
eventBusBuilder.UseIsolationUoW<CustomDbContext>(
isolationBuilder => isolationBuilder.UseMultiEnvironment("env", new List<IEnvironmentParserProvider>()
{
new EnvironmentVariablesParserProvider() //By default, environment information in environment isolation is obtained from system environment variables
}),
dbOptions => dbOptions.UseSqlServer());// Use environment isolation
});