简体   繁体   中英

What tables are needed for vanilla MVC project login?

I'm starting to learn MVC and I've figured out that the following part of the default, vanilla MVC-project manages the actual call to the database for registration.

public async Task<ActionResult> Register(RegisterViewModel model)
{
  if (ModelState.IsValid)
  {
    var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
    var result = await UserManager.CreateAsync(user, model.Password);
    if (result.Succeeded)
    {
      await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
      return RedirectToAction("Index", "Home");
    }
    AddErrors(result);
  }
  return View(model);
}

However, as I get to this line:

var result = await UserManager.CreateAsync(user, model.Password);

I'm getting an exception and as far I can understand it's because there are no tables in the DB that correspond to the model.

The model backing the 'ApplicationDbContext' context has changed since the database was created.

I don't want to use Code First. Can I manually create my tables and still be able to use the UserManager thingy? What would the tables/columns be called?

Conversely, where in my project can I control which tables/columns in an existing DB that are being read?

I'm kind of confused at the moment and after a few hours of research, I realize that I need a few pointers. I've done work with EF so I'm used to finding a model that corresponds to the DB schema but in this case, I can't find any classes that have properties similar to Username and Password etc...

First, you do want to use Code First, even if you're doing Code First with an existing database (Yeah, I know. It seems contradictory, but it's totally acceptable). Model First and Database First are deprecated.

Second, if your issue is that you have an existing database, then your best bet is to create a separate temporary MVC 5 with Individual Auth project, let that create its database, and then copy the tables over into your existing database.

I'm still pretty new to MVC, but I have been playing with MVC5 for some time. What I have found is that by default, it's going to do code first. I haven't tried switching it to something else, but when you run the program for the first time it builds the database for you. Any changes to how it runs would require you to update database and/or enable migrations as well. It has a number of fields it uses by default, but i would suggest to run it so it can build the database for you.

For your question on where it's creating the tables, most of that is done behind the scenes in the NuGet Packages it uses for authentication. Think of it like if you included System.Linq; to use some Linq in your code, it handles the rest for you. Good thing to note down, if you want to change the tables a bit, go to your IdentityModels.cs file in the models folder, and scroll down to the OnModelCreation(DbModelBuilder modelBuilder) function. That is where it modifies and adds tables or columns to the database. Each time you change that, you will need to update the database (Package Manager Console > Update-database).

The other thing to consider, if you do not want it to do that, you will want to create an MVC project that does not include authentication and do that part from scratch.

If those don't help, what were you expecting to do with it?

Also to give you some resources to check, microsoftvirtualacademy.com has some videos going over MVC5, but it's worth checking out if you are just getting into MVC.

Edit: I should also point out, MVC will automatically handle everything for you in the generated database. There are ways to modify how it works, but overall the calls it makes are all done automatically. Anything extra you can code in functions and SQL Queries for you to use that is separate, and let MVC handle the authentication database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM