Reputation: 21
I have created an asp.net MVC website with databases. I recently added an identity database. Everything works fine on my machine. However, when I run it on another computer it gives me this error:
System.Data.SqlClient.SqlException: 'Invalid object name 'AspNetUsers'.'
This is my 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);
}
}
This is my 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);
}
}
}
The error when run on another computer always generates itself here:
public AccountController(UserManager<IdentityUser> userMgr,
SignInManager<IdentityUser> signInMgr)
{
userManager = userMgr;
signInManager = signInMgr;
IdentitySeedData.EnsurePopulated(userMgr).Wait();
}
A link to the project:
https://github.com/DemarioDouce/SoccerClub
I would love to know how to resolve this error.
Upvotes: 0
Views: 4867
Reputation: 71
I had this issue as well. Another alternative solution that I haven't seen mentioned is to just create a new database migration. If you have the EF Core CLI tools installed, you can just run dotnet ef migrations add AddedEFCoreTables
followed by dotnet ef database update
. This will add the SQL tables needed for user authentication with ASP.NET Core Identity and EF Core. However, I think this is only useful if you're working with a persistent database and don't want to keep building up the database every time.
Upvotes: 2
Reputation: 21
Here is the solution:
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<AppIdentityDbContext>();
context.Database.EnsureDeleted();
context.Database.Migrate();
}
just add this to the startup.cs within here:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
}
Upvotes: 2
Reputation: 346
I go through same issue. some time user create customize class for membership hence asp.net dont create AspNetUsers table.
For example :
public class Member : IdentityUser
{
Public int MemberID { get; set; }
public string Name { get; set; }
public string Family { get; set; }
public string Password { get; set; }
}
Try to tag your IdentityUser table
[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; }
}
I hope this works for you as it did for me.
Upvotes: 3