我有多个上下文,其中一个有效,但其他三个无法找到。
我正在使用PM> Add-Migration InitialCreateLdapIdentity -context LdapIdentityDbContext<LdapUser, LdapUserRole> -verbose
这是它输出的内容:
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding BuildWebHost method...
Using environment 'Development'.
Using application service provider from BuildWebHost method on 'Program'.
Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using 'C:\Users\brech\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
Found DbContext 'ApplicationIdentityDbContext<ApplicationUser, ApplicationUserRole>'.
Found DbContext 'LdapIdentityDbContext<LdapUser, LdapUserRole>'.
Found DbContext 'BlogIdentityDbContext<BlogUser, BlogUserRole>'.
Found DbContext 'CountriesDbContext'.
Finding DbContext classes in the project...
Microsoft.EntityFrameworkCore.Design.OperationException: No DbContext named 'LdapIdentityDbContext<LdapUser, LdapUserRole>' was found.
有趣的是,当我运行Add-Migration InitialCreateCountries -context CountriesDbContext
时,这就行了。
我也尝试过没有通用参数Add-Migration InitialCreateLdapIdentity -context LdapIdentityDbContext
,但无济于事。
我的Startup.cs
包含此内容:
services
.AddDbContext<ApplicationIdentityDbContext<ApplicationUser, ApplicationUserRole>>(
options =>
{
options.UseSqlServer(Configuration.GetConnectionString("Identity"));
})
.AddDbContext<LdapIdentityDbContext<LdapUser, LdapUserRole>>(
options =>
{
options.UseSqlServer(Configuration.GetConnectionString("LdapIdentity"));
})
.AddDbContext<BlogIdentityDbContext<BlogUser, BlogUserRole>>(
options =>
{
options.UseSqlServer(Configuration.GetConnectionString("BlogIdentity"));
})
.AddDbContext<CountriesDbContext>(
options =>
{
options.UseSqlServer(Configuration.GetConnectionString("Countries"));
});
services
.AddApplicationIdentity<ApplicationUser, ApplicationUserRole>()
.AddEntityFrameworkStores<ApplicationIdentityDbContext<ApplicationUser, ApplicationUserRole>>()
.AddClaimsPrincipalFactory<UserClaimsPrincipalFactory<ApplicationUser>>()
.AddRoleStore<ApplicationRoleStore<ApplicationUser, ApplicationUserRole>>()
.AddUserStore<ApplicationUserStore<ApplicationUser, ApplicationUserRole>>()
.AddSignInManager<ApplicationSignInManager<ApplicationUser, ApplicationUserRole>>()
.AddUserManager<ApplicationUserManager<ApplicationUser, ApplicationUserRole>>()
.AddDefaultTokenProviders();
services
.AddLdapIdentity<LdapUser, LdapUserRole>()
.AddEntityFrameworkStores<LdapIdentityDbContext<LdapUser, LdapUserRole>>()
.AddClaimsPrincipalFactory<UserClaimsPrincipalFactory<LdapUser>>()
.AddUserStore<LdapUserWtore<LdapUser, LdapUserRole>>()
.AddSignInManager<LdapSignInManager<LdapUser, LdapUserRole>>()
.AddUserManager<LdapUserManager<LdapUser, LdapUserRole>>()
.AddDefaultTokenProviders();
services
.AddBlogIdentity<BlogUser, BlogUserRole>()
.AddEntityFrameworkStores<BlogIdentityDbContext<BlogUser, BlogUserRole>>()
.AddClaimsPrincipalFactory<UserClaimsPrincipalFactory<BlogUser>>()
.AddUserStore<BlogUserStore<BlogUser, BlogUserRole>>()
.AddUserManager<BlogUserManager<BlogUser, BlogUserRole>>()
.AddDefaultTokenProviders();
我完全没有想法,特别是因为在详细日志中明确说明它找到了上下文。
任何人都有任何想法?
修改的
方法AddApplicationIdentity<…>
,AddLdapIdentity<…>
和AddBlogIdentity<…>
是ServiceCollection
上的扩展方法,我偷看了Github上的asp.net核心实现
所以我的实现看起来像这样:
public static IdentityBuilder AddApplicationIdentity<TUser, TUserRole>(
this IServiceCollection services, Action<IdentityOptions> setupAction = null)
where TUser : ApplicationUser, new()
where TUserRole : ApplicationUserRole
{
services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
services.TryAddScoped<IRoleValidator<TUserRole>, RoleValidator<TUserRole>>();
services.TryAddScoped<IdentityErrorDescriber>();
services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TUserRole>>();
services.TryAddScoped<ApplicationUserStore<TUser, TUserRole>, ApplicationUserStore<TUser, TUserRole>>();
services.TryAddScoped<ApplicationIdentityDbContext<Identity.Models.ApplicationUser, ApplicationUserRole>, ApplicationIdentityDbContext<Identity.Models.ApplicationUser, ApplicationUserRole>>();
services.TryAddScoped<ApplicationUserManager<TUser, TUserRole>, ApplicationUserManager<TUser, TUserRole>>();
services.TryAddScoped<ApplicationSignInManager<TUser, TUserRole>, ApplicationSignInManager<TUser, TUserRole>>();
services.TryAddScoped<RoleManager<TUserRole>, AspNetRoleManager<TUserRole>>();
if (setupAction != null)
{
services.Configure(setupAction);
}
return new IdentityBuilder(typeof(TUser), typeof(TUserRole), services);
}
public static IdentityBuilder AddLdapIdentity<TUser, TUserRole>(
this IServiceCollection services, Action<IdentityOptions> setupAction = null)
where TUser : LdapUser, new()
where TUserRole : LdapUserRole
{
services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
services.TryAddScoped<IRoleValidator<TUserRole>, RoleValidator<TUserRole>>();
services.TryAddScoped<IdentityErrorDescriber>();
services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TUserRole>>();
services.TryAddScoped<LdapUserStore<TUser, TUserRole>, LdapUserStore<TUser, TUserRole>>();
services.TryAddScoped<LdapIdentityDbContext<TUser, TUserRole>, LdapIdentityDbContext<TUser, TUserRole>>();
services.TryAddScoped<LdapUserManager<TUser, TUserRole>, LdapUserManager<TUser, TUserRole>>();
services.TryAddScoped<LdapSignInManager<TUser, TUserRole>, LdapSignInManager<TUser, TUserRole>>();
services.TryAddScoped<RoleManager<TUserRole>, AspNetRoleManager<TUserRole>>();
if (setupAction != null)
{
services.Configure(setupAction);
}
return new IdentityBuilder(typeof(TUser), typeof(TUserRole), services);
}
public static IdentityBuilder AddBlogIdentity<TUser, TUserRole>(
this IServiceCollection services, Action<IdentityOptions> setupAction = null)
where TUser : BlogUser, new()
where TUserRole : BlogUserRole
{
services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
services.TryAddScoped<IRoleValidator<TUserRole>, RoleValidator<TUserRole>>();
services.TryAddScoped<IdentityErrorDescriber>();
services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TUserRole>>();
services.TryAddScoped<BlogUserStore<TUser, TUserRole>, BlogUserStore<TUser, TUserRole>>();
services.TryAddScoped<BlogIdentityDbContext<BlogUser, BlogUserRole>, BlogIdentityDbContext<BlogUser, BlogUserRole>>();
services.TryAddScoped<BlogUserManager<TUser, TUserRole>, BlogUserManager<TUser, TUserRole>>();
services.TryAddScoped<RoleManager<TUserRole>, AspNetRoleManager<TUserRole>>();
if (setupAction != null)
{
services.Configure(setupAction);
}
return new IdentityBuilder(typeof(TUser), typeof(TUserRole), services);
}
}
LdapIdentityDbContext
的实施是这样的:
public class LdapIdentityDbContext<TUser, TRole> : IdentityDbContext<TUser, TUserRole, Guid>
where TUser : LdapUser
where TRole : LdapUserRole
{
public LdapIdentityDbContext(DbContextOptions<LdapIdentityDbContext<TUser, TRole>> options)
: base(options)
{
}
}
答案 0 :(得分:1)
找到它,结果证明你不能在IdentityDbContext中使用类型参数。