entity-framework - 无法为 ApplicationUser 创建 DbSet
问题描述
我有一个我正在重构的 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 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。
任何指针?
解决方案
推荐阅读
- postman - 使用文本分析 API 和 Postman 进行情绪分析
- html - 在 HTML 输入模式属性中转义“和”
- spring - 将 Spring Data JPA 与 Spring JPA 一起使用时的问题
- kotlin - kotlin 中 == 和 === 有什么区别
- perl - 在 Mojo 中查找以特定 url 开头的链接
- html - 全屏居中 html5 视频直播
- azure-devops - 如何限制对 VSTS 中特定工作项功能的访问
- javascript - 动作不触发reducer
- excel - VBA添加具有可变范围的小计
- menuitem - 错误:找不到函数“createRenderFunction”