.net core Identity web 项目创建(三) 如果 多个不同类型数据库 如何 对接Identity
将 上一篇的 ef identity mysql 抽取出来

ExtendEF 项目代码
各种引用 如下
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" />
</ItemGroup>
</Project>
/// <summary>
/// 扩展 角色
/// </summary>
public class AppliactionRole : IdentityRole
{
/// <summary>
/// 角色别名
/// </summary>
public string NickName { get; set; }
}
/// <summary>
/// 扩展用户
/// </summary>
public class AppliactionUser : IdentityUser
{
/// <summary>
/// 用户所在城市
/// </summary>
public string City { get; set; }
/// <summary>
/// 用户昵称
/// </summary>
public string NickName { get; set; }
}
/// <summary>
/// 扩展用户 和 角色
/// </summary>
public class AppliactionDBContext : IdentityDbContext<AppliactionUser, AppliactionRole, string>
{
public AppliactionDBContext(DbContextOptions<AppliactionDBContext> options) : base(options)
{
}
}
ExtendMySql 项目代码
各种引用
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ExtendEF\ExtendEF.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
public class AppliactionDBContextExtend : IDesignTimeDbContextFactory<AppliactionDBContext>
{
public AppliactionDBContext CreateDbContext(string[] args)
{
ConfigurationBuilder configuration = new ConfigurationBuilder();
configuration.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
configuration.AddJsonFile("appsettings.json", true,true);
var root = configuration.Build();
var conStr = root.GetConnectionString("DefaultConnection");
var builder = new DbContextOptionsBuilder<AppliactionDBContext>();
var serverVersion = new MySqlServerVersion(new Version(8, 0, 27));
builder.UseMySql(conStr, serverVersion, mysqlOptions => mysqlOptions.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().FullName));
return new AppliactionDBContext(builder.Options);
}
}
appsettings.json
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5"
},
"exclude": [
"node_modules",
"wwwroot"
],
"ConnectionStrings": {
"DefaultConnection": "Server=192.168.0.196;Database=TestIdentity2;uid=root;pwd=123456"
}
}
设置 ExtendMySql 为启动项目
程序包管理平台 设置默认项目也为 ExtendMySql
add-migration extend_user1
update-database

查看 数据库生成


浙公网安备 33010602011771号