Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.net mvc错误System.Data.SqlClient.SqlException:';无效的对象名称';AspNetUsers'';_C#_Asp.net_Asp.net Mvc_Model View Controller - Fatal编程技术网

C# Asp.net mvc错误System.Data.SqlClient.SqlException:';无效的对象名称';AspNetUsers'';

C# Asp.net mvc错误System.Data.SqlClient.SqlException:';无效的对象名称';AspNetUsers'';,c#,asp.net,asp.net-mvc,model-view-controller,C#,Asp.net,Asp.net Mvc,Model View Controller,我已经创建了一个带有数据库的asp.net MVC网站。我最近添加了一个身份数据库。我的机器一切正常。但是,当我在另一台计算机上运行它时,会出现以下错误: System.Data.SqlClient.SqlException:“无效的对象名'AspNetUsers'。 这是我的AccountController public class AccountController : Controller { private UserManager<IdentityUse

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

System.Data.SqlClient.SqlException:“无效的对象名'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);
        }
    }
公共类AccountController:控制器
{
私有用户管理器用户管理器;
私人签名管理员签名管理员;
公共帐户控制器(UserManager userMgr,
签名管理员(签名管理器)
{
userManager=userMgr;
signInManager=signInMgr;
IdentitySeedData.EnsurePopulated(userMgr.Wait();
}
[异名]
公共视图结果登录(字符串返回URL)
{
返回视图(新的LoginModel)
{
ReturnUrl=ReturnUrl
});
}
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务登录(LoginModel LoginModel)
{
if(ModelState.IsValid)
{
识别用户=
等待userManager.FindByNameAsync(loginModel.Name);
如果(用户!=null)
{
等待signInManager.SignOutAsync();
如果((等待signInManager.PasswordSignInAsync)(用户,
loginModel.Password,false,false))。成功)
{
返回重定向(loginModel?.ReturnUrl???/Admin/Index);
}
}
}
AddModelError(“,“无效的名称或密码”);
返回视图(loginModel);
}
公共异步任务注销(字符串returnUrl=“/”)
{
等待signInManager.SignOutAsync();
返回重定向(returnUrl);
}
}
这是我的身份识别数据

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 class IdentitySeedData
{
私有常量字符串adminUser=“Admin”;
private const string adminPassword=“Secret123$”;
已重新填充公共静态异步任务(UserManager UserManager)
{
IdentityUser user=await userManager.FindByIdAsync(adminUser);
if(user==null)
{
用户=新标识用户(“管理员”);
等待userManager.CreateAsync(用户,adminPassword);
}
}
}
在另一台计算机上运行时,错误始终在此处生成:

 public AccountController(UserManager<IdentityUser> userMgr,
                       SignInManager<IdentityUser> signInMgr)
        {
            userManager = userMgr;
            signInManager = signInMgr;
            IdentitySeedData.EnsurePopulated(userMgr).Wait();
        }
public AccountController(UserManager userMgr,
签名管理员(签名管理器)
{
userManager=userMgr;
signInManager=signInMgr;
IdentitySeedData.EnsurePopulated(userMgr.Wait();
}
该项目的链接:


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

我也遇到过同样的问题。有时用户会为成员身份创建自定义类,因此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; }

}

我希望这对你和对我一样有效。

我也经历过同样的问题。有时用户会为成员身份创建自定义类,因此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; }

}
我希望这对您和对我一样有效。

以下是解决方案:

using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
    var context = serviceScope.ServiceProvider.GetRequiredService<AppIdentityDbContext>();
    context.Database.EnsureDeleted();
    context.Database.Migrate();
}
以下是解决方案:

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

谢谢你的回复。如图所示,我添加了一个成员类。然而,我也犯了同样的错误。我将添加一个截图。感谢您的回复。如图所示,我添加了一个成员类。然而,我也犯了同样的错误。我将添加一个屏幕截图。