EF Core“没有找到名为 xxx 的 DbContext”

EF Core "No DbContext named xxx was found'

我有多个上下文,其中一个有效,但找不到其他三个。

我正在使用 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 上的扩展方法,我已经查看了 [=23] 上的 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)
    {
    }
}

找到了,原来你不能在 IdentityDbContext 中使用类型参数。