【问题标题】:Asp.net mvc error System.Data.SqlClient.SqlException: 'Invalid object name 'AspNetUsers'.'Asp.net mvc 错误 System.Data.SqlClient.SqlException:'无效的对象名称'AspNetUsers'。
【发布时间】:2020-04-01 17:20:27
【问题描述】:

我创建了一个带有数据库的 asp.net MVC 网站。我最近添加了一个身份数据库。在我的机器上一切正常。但是,当我在另一台计算机上运行它时,它给了我这个错误:

System.Data.SqlClient.SqlException: 'Invalid object name 'AspNetUsers'.'

这是我的 AccountController

public class AccountController : Controller
    {
        private UserManager<IdentityUser> userManager;
        private SignInManager<IdentityUser> signInManager;
        public AccountController(UserManager<IdentityUser> userMgr,
                       SignInManager<IdentityUser> signInMgr)
        {
            userManager = userMgr;
            signInManager = signInMgr;
            IdentitySeedData.EnsurePopulated(userMgr).Wait();
        }

        [AllowAnonymous]
        public ViewResult Login(string returnUrl)
        {
            return View(new LoginModel
            {
                ReturnUrl = returnUrl
            });
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Login(LoginModel loginModel)
        {
            if (ModelState.IsValid)
            {
                IdentityUser user =
                await userManager.FindByNameAsync(loginModel.Name);
                if (user != null)
                {
                    await signInManager.SignOutAsync();
                    if ((await signInManager.PasswordSignInAsync(user,
                    loginModel.Password, false, false)).Succeeded)
                    {
                        return Redirect(loginModel?.ReturnUrl ?? "/Admin/Index");
                    }
                }
            }
            ModelState.AddModelError("", "Invalid name or password");
            return View(loginModel);
        }

        public async Task<RedirectResult> Logout(string returnUrl = "/")
        {
            await signInManager.SignOutAsync();
            return Redirect(returnUrl);
        }
    }

这是我的 IdentitySeedData

public class IdentitySeedData
    {
        private const string adminUser = "Admin";
        private const string adminPassword = "Secret123$";

        public static async Task EnsurePopulated(UserManager<IdentityUser> userManager)
        {

            IdentityUser user = await userManager.FindByIdAsync(adminUser);
            if (user == null)
            {
                user = new IdentityUser("Admin");
                await userManager.CreateAsync(user, adminPassword);
            }
        }
    }

在另一台计算机上运行时的错误总是在这里生成:

 public AccountController(UserManager<IdentityUser> userMgr,
                       SignInManager<IdentityUser> signInMgr)
        {
            userManager = userMgr;
            signInManager = signInMgr;
            IdentitySeedData.EnsurePopulated(userMgr).Wait();
        }

项目链接:

https://github.com/DemarioDouce/SoccerClub

我很想知道如何解决这个错误。

【问题讨论】:

    标签: c# asp.net asp.net-mvc model-view-controller


    【解决方案1】:

    我遇到了同样的问题。有时用户会为会员创建自定义类,因此 asp.net 不会创建 AspNetUsers 表。

    例如:

    public class Member : IdentityUser
    {
        Public int MemberID { get; set; }
        public string Name { get; set; }
        public string Family { get; set; }
        public string Password { get; set; }
    
    }  
    

    尝试标记您的 IdentityUser 表

    [Table("AspNetUsers")]
    public class Member : IdentityUser
    {
        Public int MemberID { get; set; }
        public string Name { get; set; }
        public string Family { get; set; }
        public string Password { get; set; }
    
    }
    

    我希望这对你有用。

    【讨论】:

    • 感谢您的回复。如图所示,我添加了一个成员类。但是,我得到了同样的错误。我将添加一个屏幕截图。
    【解决方案2】:

    解决办法如下:

    using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
    {
        var context = serviceScope.ServiceProvider.GetRequiredService<AppIdentityDbContext>();
        context.Database.EnsureDeleted();
        context.Database.Migrate();
    }
    

    只需将其添加到此处的 startup.cs 中:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多