locked
SqlException: Invalid object name 'AspNetUsers'. RRS feed

  • Question

  • User-675506398 posted

    Hello! I've seen this error message a couple of times now. I am creating a .NET Core 3.0 MVC web app for renting cars where I need 2 types of users (admin and user). The user should be able to register, login and view the cars that are managed by the admin. I have problems with login. I followed some suggestions and added migrations to my project and it still gives me this error for some reason. 

    Source error:

    CarRentingApp.Controllers.AccountController.Login(LoginViewModel loginViewModel) in AccountController.cs
    -
            [HttpPost]
            public async Task<IActionResult> Login(LoginViewModel loginViewModel)
            {
                if (!ModelState.IsValid)
                    return View(loginViewModel);
                var user = await _userManager.FindByNameAsync(loginViewModel.Username); //this line appears in red
                if (user != null)
                {
                    var result = await _signInManager.PasswordSignInAsync(user, loginViewModel.Password, false, false);
                    if (result.Succeeded)
                    {

    I have searched a lot, but none of the answers is helpful. How can I fix that?

    Sunday, December 1, 2019 12:48 AM

All replies

  • User818337214 posted

    I guess your username is null 

    if you wanna check with modelstate.isvalid you must use "required "

    example :

    public class LoginInput
        {
            [Required(ErrorMessage = "EmailRequiredError")]
            [EmailAddress(ErrorMessage = "EmailFormatError")]
            [Display(Name = "Email")]
            public string Email { get; set; }
    
            [Required(ErrorMessage = "PasswordRequiredError")]
            [DataType(DataType.Password)]
            [Display(Name = "Password")]
            public string Password { get; set; }
    
            [Display(Name = "LoginRememberMe")]
            public bool RememberMe { get; set; }
        }

    after you can see that modelstate is invalid.... 

    Sunday, December 1, 2019 1:34 AM
  • User-675506398 posted

    That doesn't help. I have this code:

    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    
    namespace CarRentingApp.Models
    {
        [Table("User")]
        public class LoginViewModel
        {
            [Required]
            public string Username { get; set; }
    
            [Required]
            [DataType(DataType.Password)]
            public string Password { get; set; }
    
            [Display(Name = "Remember me")]
            public bool RememberMe { get; set; }
    
            public string ReturnUrl { get; set; }
        }
    }
    

    Sunday, December 1, 2019 2:40 AM
  • User-1204637165 posted

    Dear Poster,

    Can we see your Database context class. This simply means that you have not scripted your database to reflect to the default login component tables.

    So the AspNetUsers table is currently not in your database that is the reason you are getting this error message.

    We need to be sure that you are extending the IdentityUser. When creating Database context.

      public class ApplicationDbContext : IdentityDbContext<IdentityUser>
        {
    
    }

    Something like that. You are also suppose to do some

        //This is going to be used when you are in need for authentication processes.
                services.AddIdentity<IdentityUser, IdentityRole>()
                    .AddEntityFrameworkStores<ApplicationDbContext>()
                    .AddDefaultTokenProviders();
    

    In your startup class.

    You are to add-migration to generate the first migration that would contain the default Login authentication table.

    then Script-Migration to generate the scripts that you run to create the first migration. Giving your that users table you getting as an invalid object.

    Hope this helps poster.

    Sunday, December 1, 2019 7:04 AM
  • User-675506398 posted

    Database context: 

    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore;
    
    namespace CarRentingApp.Models
    {
        public partial class carRentalDbContext : IdentityDbContext<User>
        {
            public carRentalDbContext()
            {
            }
    
            public carRentalDbContext(DbContextOptions<carRentalDbContext> options)
                : base(options)
            {
            }

    Startup: 

     public void ConfigureServices(IServiceCollection services)
            {
                var connection = Configuration.GetConnectionString("CarRentalDB");
                services.AddDbContext<carRentalDbContext>(options => options.UseSqlServer(connection));
    
                services.AddIdentity<User, IdentityRole>()
                .AddEntityFrameworkStores<carRentalDbContext>()
                .AddDefaultTokenProviders();
    
                services.AddControllersWithViews();
            }

    Sunday, December 1, 2019 6:01 PM
  • User-1204637165 posted

    Your Database context seems correct. When you do your add-migrations in your first migrations.

    You are meant to run Script-Migrations

    After you are generated your first migration. Then run the script on your database.

    Check if the Aspnetusers table is created.

    I suggest you delete your old database and do this owl process from start and see what happens. Let me know how it goes.

    Thanks.

    Sunday, December 1, 2019 6:23 PM