更新时间:2023-02-25 21:46:51
如果您按照向用户添加自定义字段的所有步骤进行操作,您将成功完成任务.
If you follow all steps of adding a custom field to user, you will finish the tasks successfully.
以下是向用户添加自定义字段的所有步骤:
Here is all steps to add a custom field to user:
转到 Models 文件夹 → 打开 IdentityModels.cs → ApplicationUser 类并添加属性:
Go to Models folder → Open IdentityModels.cs → ApplicationUser class and add the property:
public string Code { get; set; }
输入 Enable-Migrations
并按 Enter 并等待任务完成.您将看到一条回复,内容为:
Type Enable-Migrations
and press Enter and wait until the task get completed. You will see a response which says:
输入 Add-Migration "Code"
并按 Enter 并等待任务完成.您将看到一条回复,内容为:
Type Add-Migration "Code"
and press Enter and wait until the task get completed. You will see a response which says:
输入 Update-Database
并按 Enter 并等待任务完成.您将看到一条回复,内容为:
Scaffolding migration 'Code'. The Designer Code for this migration
file includes a snapshot of your current Code First model. This
snapshot is used to calculate the changes to your model when you
scaffold the next migration. If you make additional changes to your
model that you want to include in this migration, then you can
re-scaffold it by running 'Add-Migration Code' again.
在此步骤中,如果您刷新SQL Server 对象资源管理器 并转到数据库并查看表,则在列下的 dbo.AspNetUsers
下,您将看到 Code
字段.如果您不知道应该查找哪个数据库甚至哪个服务器,请打开 Web.Config 文件并查看如下所示的连接字符串:
Specify the '-Verbose' flag to view the SQL statements being applied
to the target database.
Applying explicit migrations: [201611132135242_Code].
Applying explicit migration: 201611132135242_Code.
Running Seed method.
您可以看到数据源(即 sql server 实例)和一些 .mdf 内容,即数据库名称.
转到 Models 文件夹 → 打开 AccountViewModels.cs 文件 → RegisterViewModel 类并添加此属性:(在带有 EF6 的 APIv2 中,您可以在 Models 文件夹 → AccountBindingModels 文件 → RegisterBindingModel 类中添加以下行)
At this step if you refresh SQL Server Object Explorer and go to database and see tables, under dbo.AspNetUsers
under columns, you will see the Code
field. If you didn't know which database or even which server you should look for, open Web.Config file and take a look at connection string which is something like this:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)v11.0;AttachDbFilename=|DataDirectory|aspnet-WebApplication1-20161114125903.mdf;Initial Catalog=aspnet-WebApplication1-20161114125903;Integrated Security=True"
providerName="System.Data.SqlClient" />
转到 Views 文件夹 → Account 文件夹 → 打开 Register.cshtml 文件并在其他字段附近添加此代码,例如以下密码:
You can see data source (which is sql server instance) and something .mdf which is database name.
Go to Models folder → Open AccountViewModels.cs file → RegisterViewModel class and add this property:
(In APIv2 with EF6, you can add the below line in Models folder → AccountBindingModels file → RegisterBindingModel class)
public string Code { get; set; }
转到 Controllers 文件夹 → 打开 AccountController.cs 文件 → 在 http post Register 操作中,更改创建用户的行对此:
Go to Controllers folder → Open AccountController.cs file → in http post Register action, change the line which creates user to this:
var user = new ApplicationUser { UserName = model.Email, Email = model.Email,
Code= model.Code };
运行项目并转到 /Account/Register
url 并注册一个新用户.注册用户后,再次进入数据库,查看dbo.AspNetUsers表的数据,可以看到代码已经保存了.
Run project and go to /Account/Register
url and register a new user. After registering the user, if you go to database again and View Data of dbo.AspNetUsers table, you will see the code has been saved.
下载
您可以在此处克隆或下载一个工作示例:
You can clone or download a working example here:
进一步阅读 - 如何向 IdentityRole 添加自定义属性?
如果您有兴趣了解如何向 IdentityRole
添加新属性,请查看 How向 IdentityRole 添加自定义属性?
If you are interested to know how to add a new property to IdentityRole
, take a look at How to Add a custom Property to IdentityRole?