【发布时间】:2018-07-09 16:27:54
【问题描述】:
我有多个上下文,其中一个有效,但找不到其他三个。
我正在使用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)
{
}
}
【问题讨论】:
-
由于您已经使用
AddDbContext添加了LdapIdentityDbContext、BlogIdentityDbContext和ApplicationIdentityDbContext,因此您无需在扩展方法中添加它们的作用域版本(dbContexts 添加为瞬态)。 -
那没有做到,在我遇到问题之前,我有另一个项目的身份数据库,一切正常,但是我更改了连接字符串,以便我有单独的数据库,并且当我想要的时候为那些单独的上下文创建迁移问题开始了,所以我非常怀疑这是问题所在,但无论如何,谢谢,我会删除它。 :-)
标签: c# entity-framework-core ef-core-2.0