简体   繁体   English

vanilla MVC 项目登录需要哪些表?

[英]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.我开始学习 MVC,我发现默认的以下部分,vanilla MVC-project 管理对数据库的实际调用以进行注册。

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.自数据库创建以来,支持“ApplicationDbContext”上下文的模型已更改。

I don't want to use Code First.我不想使用代码优先。 Can I manually create my tables and still be able to use the UserManager thingy?我可以手动创建我的表并且仍然能够使用UserManager东西吗? 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...我已经完成了 EF 的工作,所以我习惯于查找与 DB 模式相对应的模型,但在这种情况下,我找不到任何具有类似于用户名密码等属性的类...

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).首先,您确实希望使用 Code First,即使您正在使用现有数据库执行 Code First(是的,我知道。这似乎矛盾,但完全可以接受)。 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.其次,如果您的问题是您有一个现有的数据库,那么最好的办法是创建一个单独的临时 MVC 5 与个人身份验证项目,让它创建它的数据库,然后将表复制到您现有的数据库中。

I'm still pretty new to MVC, but I have been playing with MVC5 for some time.我对 MVC 还是很陌生,但我已经使用 MVC5 有一段时间了。 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.对于您在何处创建表的问题,其中大部分是在用于身份验证的 NuGet 包中的幕后完成的。 Think of it like if you included System.Linq;想象一下,如果您包含System.Linq; to use some Linq in your code, it handles the rest for you.要在您的代码中使用一些 Linq,它会为您处理其余的工作。 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.需要注意的是,如果您想稍微更改表,请转到模型文件夹中的 IdentityModels.cs 文件,然后向下滚动到OnModelCreation(DbModelBuilder modelBuilder)函数。 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.要考虑的另一件事是,如果您不希望它这样做,您将需要创建一个不包括身份验证的 MVC 项目并从头开始执行该部分。

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.另外,为了给您提供一些资源供您查看,microsoftvirtualacademy.com 有一些关于 MVC5 的视频,但如果您刚刚接触 MVC,则值得查看。

Edit: I should also point out, MVC will automatically handle everything for you in the generated database.编辑:我还应该指出,MVC 会自动为您处理生成的数据库中的所有内容。 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.您可以在函数和 SQL 查询中编码以供您使用的任何额外内容都是单独的,并让 MVC 处理身份验证数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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