InvalidOperationException:无法为“ ApplicationUser”创建DbSet,因为此类型不包含在上下文模型中

时间:2018-09-20 14:30:31

标签: c# asp.net-core-2.1

我有一个摇摇晃晃的asp.net core 2.1应用程序,无论何时尝试登录或注册新用户,我都会不断收到以下错误消息:“ InvalidOperationException:无法为'ApplicationUser'创建DbSet,因为此类型不包括在内上下文模型。”

出现_signInManager.PasswordSignInAsync()时,它就会爆发。

我陷入困境,无法理解需要进行哪些更改。尝试我们链接,但没有解决。

这是我的login.cs

 public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = "/RealTimeChart/Index")
    {
        ViewData["ReturnUrl"] = returnUrl;
        if (ModelState.IsValid)
        {
            //ApplicationUser signedUser = await _userManager.FindByEmailAsync(model.Email);
            //var result = await _signInManager.PasswordSignInAsync(signedUser.UserName, model.Password, model.RememberMe, false);

            var signedUser = await _userManager.FindByEmailAsync(model.Email);
            var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: true);

            if (result.Succeeded)
            {
                _logger.LogInformation("User logged in.");
                return RedirectToLocal(returnUrl);
            }
            if (result.IsLockedOut)
            {
                _logger.LogWarning("User account locked out.");
                return RedirectToAction(nameof(Lockout));
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                return View(model);
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

我的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 Strength Setting
        services.Configure<IdentityOptions>(options =>
        {
            // Lockout settings
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
            options.Lockout.MaxFailedAccessAttempts = 10;
            options.Lockout.AllowedForNewUsers = true;

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

        //Seting the Account Login page
        services.ConfigureApplicationCookie(options =>
        {
            // Cookie settings
            options.Cookie.HttpOnly = true;
            options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
            options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
            options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
            options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
            options.SlidingExpiration = true;

        });

        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();
        // Add services
        services.AddTransient<IContactManagement, ContactManagement>();
        services.AddTransient<IRepository<Contact>, Repository<Contact>>();
        services.AddTransient<IJobManagement, JobJobManagement>();
        services.AddTransient<IRepository<Job>, Repository<Job>>();
        services.AddTransient<IUnitManagement, UnitManagement>();
        services.AddTransient<IRepository<Sensor>, Repository<Sensor>>();
        services.AddTransient<IJobContactManagement, JobContactManagement>();
        services.AddTransient<IRepository<JobContact>, Repository<JobContact>>();
        services.AddTransient<IJobContactEventManagement, JobContactEventManagement>();
        services.AddTransient<IRepository<JobContactEvent>, Repository<JobContactEvent>>();
        services.AddTransient<IJobsensorManagement, JobsensorManagement>();
        services.AddTransient<IRepository<JobSensor>, Repository<JobSensor>>();
        services.AddTransient<IEventTypesManagement, EventTypesManagement>();
        services.AddTransient<IRepository<EventType>, Repository<EventType>>();
        services.AddTransient<IJobSensorLocationPlannedManagement, JobSensorLocationPlannedManagement>();
        services.AddTransient<IRepository<JobSensorLocationPlanned>, Repository<JobSensorLocationPlanned>>();
        services.AddTransient<IDataTypeManagement, DataTypeManagement>();
        services.AddTransient<IRepository<DataType>, Repository<DataType>>();
        services.AddTransient<IThresholdManegement, ThresholdManegement>();
        services.AddTransient<IRepository<Threshold>, Repository<Threshold>>();
        services.AddTransient<ISeverityTypeManagement, SeverityTypeManagement>();
        services.AddTransient<IRepository<SeverityType>, Repository<SeverityType>>();
        services.AddTransient<ISensorDataManagement, SensorDataManagement>();
        services.AddTransient<IRepository<SensorData>, Repository<SensorData>>();
        services.AddTransient<ISensorLocationManagement, SensorLocationManagement>();
        services.AddTransient<IRepository<SensorLocation>, Repository<SensorLocation>>();

        services.AddTransient<ISensorStatusTypeManagement, SensorStatusTypeManagement>();
        services.AddTransient<IRepository<SensorStatusType>, Repository<SensorStatusType>>();
        services.AddTransient<IRepository<SensorDataToday>, Repository<SensorDataToday>>();
        services.AddTransient<ISensorDataTodayManagement, SensorDataTodayManagement>();

        services.AddMvc();
    }

我的ApplicationDbContext.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
       : base(options)
    {
    }

   // public object Jobs { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }

    public DbSet<DataType> DataTypes { get; set; }
    public DbSet<UserAccountType> UserAccountTypes { get; set; }
    public DbSet<GeneralSetting> GeneralSettings { get; set; }
    public DbSet<Job> Jobs { get; set; }
    public DbSet<Log> Logs { get; set; }
    public DbSet<Sensor> Sensors { get; set; }
    public DbSet<SensorDataRaw> SensorDataRaw { get; set; }
    public DbSet<SensorDataRawArchive> SensorDataRawArchive { get; set; }
    public DbSet<SensorStatusType> SensorStatusTypes { get; set; }
    public DbSet<SeverityType> SeverityTypes { get; set; }
    public DbSet<StatusReportType> StatusReportTypes { get; set; }
    public DbSet<Contact> Contacts { get; set; }
    public DbSet<EventType> EventTypes { get; set; }
    public DbSet<Event> Events { get; set; }
    public DbSet<JobContact> JobContacts { get; set; }
    public DbSet<JobContactEvent> JobContactEvents { get; set; }
    public DbSet<JobContactStatusReport> JobContactStatusReports { get; set; }
    public DbSet<JobSensor> JobSensors { get; set; }
    public DbSet<JobSensorLocationPlanned> JobSensorLocationPlanned { get; set; }
    public DbSet<Notification> Notifications { get; set; }
    public DbSet<SensorData> SensorData { get; set; }
    public DbSet<SensorDataTemp> SensorDataTemp { get; set; }
    public DbSet<SensorLocation> SensorLocations { get; set; }
    public DbSet<Threshold> Thresholds { get; set; }
    public DbSet<SensorDataToday> SensorDataToday { get; set; }

}

public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
    public ApplicationDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
        optionsBuilder.UseSqlServer("Data Source=NEWSQL\\NEWSQLSERVER;Initial Catalog=RM;User ID=sa;Password=Speak2me");
        return new ApplicationDbContext(optionsBuilder.Options);
    }
}

我们非常感谢您的帮助

0 个答案:

没有答案