无法为ApplicationUser

时间:2018-12-10 11:31:21

标签: entity-framework asp.net-core-mvc asp.net-identity

我有一个正在重构的asp.net核心项目。以前,我的所有数据库逻辑都包含在该项目中,但是当我们现在添加WebAPI时,我已经将数据库逻辑移到了一个单独的.net核心标准项目中,因此在两个项目之间共享。

这在新的Web api中似乎可以正常工作,但是我在原始项目中遇到了与signInManager和ApplicationUser类有关的问题。

所有编译都很好,但是在运行时出现以下错误:

InvalidOperationException: Cannot create a DbSet for 'ApplicationUser' because this type is not included in the model for the context.`

我还将这个ApplicationUser类移到了新的DAL项目中,据我所知,我已经更新了对它的所有引用(肯定足以通过编译时检查)。

我的startup.cs如下:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // password policy settings
        services.Configure<IdentityOptions>(options =>
        {
            // Password settings
            options.Password.RequireDigit = true;
            options.Password.RequiredLength = 8;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = true;
            options.Password.RequireLowercase = false;
            options.Password.RequiredUniqueChars = 6;

            // Lockout settings
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
            options.Lockout.MaxFailedAccessAttempts = 10;
            options.Lockout.AllowedForNewUsers = true;

            // User settings
            options.User.RequireUniqueEmail = true;

        });

        // Add session cookie
        services.ConfigureApplicationCookie(options =>
        {
            // Cookie settings
            options.Cookie.HttpOnly = true;
            options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
            // If the LoginPath isn't set, ASP.NET Core defaults 
            // the path to /Account/Login.
            options.LoginPath = "/Account/Login";
            // If the AccessDeniedPath isn't set, ASP.NET Core defaults 
            // the path to /Account/AccessDenied.
            options.AccessDeniedPath = "/Account/AccessDenied";
            options.SlidingExpiration = true;

            // Disable redirect to login page for unauthorized requests to / api resources
            options.Events.OnRedirectToLogin = context =>
            {
                if (context.Request.Path.StartsWithSegments("/api") && context.Response.StatusCode == StatusCodes.Status200OK)
                {
                    context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                    return Task.FromResult<object>(null);
                }
                else
                {
                    context.Response.Redirect(context.RedirectUri);
                    return Task.FromResult<object>(null);
                }
            };

        });

        // Add application services.
        services.AddTransient<IEmailSender, EmailSender>();

        // add our Db handlers
        services.AddScoped<DAL.Services.Interfaces.IServices, DAL.Services.Services>();

        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider services)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        var cultureInfo = new CultureInfo("en-GB");

        CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
        CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

        app.UseStaticFiles();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });


    }


}

我在顶部有一个“ using”语句,如下所示:

using IceBowl.DAL; using IceBowl.DAL.Models;

因此,对“ AddIdentity”的调用正在正确的ApplicationUser中传递-实际上,只有一个ApplicationUser类,我删除了原始类。

代码似乎在以下行出现问题:

var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);

关于登录管理器有些不喜欢,但我不知所措。所有引用均已更新,现在指向包含datacontext和ApplicationUser类的DAL。

有指针吗?

0 个答案:

没有答案