EF Core "No DbContext named xxx was found'

c# ef-core-2.0 entity-framework-core

Question

I have multiple contexts and one of them works, but the other three cannot be found.

I'm using PM> Add-Migration InitialCreateLdapIdentity -context LdapIdentityDbContext<LdapUser, LdapUserRole> -verbose

This is what it outputs:

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.

The funny thing is when I run Add-Migration InitialCreateCountries -context CountriesDbContext then this works.

I also tried without the generic parameters Add-Migration InitialCreateLdapIdentity -context LdapIdentityDbContext but to no avail.

My Startup.cs contains this:

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();

I'm all out of ideas, especially because in the verbose log it says explicitly that it found the contexts.

Anyone has any ideas?

edit

The methods AddApplicationIdentity<…>, AddLdapIdentity<…> and AddBlogIdentity<…> are extension methods on ServiceCollection and I have peeked at the asp.net core implementation on Github

So my implementation looks like this:

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);
    }
}

The implementation of LdapIdentityDbContext is this:

public class LdapIdentityDbContext<TUser, TRole> : IdentityDbContext<TUser, TUserRole, Guid>
    where TUser : LdapUser
    where TRole : LdapUserRole
{
    public LdapIdentityDbContext(DbContextOptions<LdapIdentityDbContext<TUser, TRole>> options)
        : base(options)
    {
    }
}
1
2
1/31/2018 9:58:18 AM

Accepted Answer

Found it, turns out you cannot use type parameters in the IdentityDbContext.

2
1/31/2018 11:20:41 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow