简体   繁体   English

如何在Asp.Net MVC 4 Simple Membership的用户配置文件中添加FistName和LastName的其他字段?

[英]How to Add Extra Fields of FistName and LastName in Userprofile of Asp.Net MVC 4 Simple Membership?

I want to add 2 Extra fields in UserProfile Asp.Net Simple membership, But unable to add and not find any help from internet. 我想在UserProfile Asp.Net简单成员身份中添加2个额外字段,但无法添加,也无法从Internet找到任何帮助。 Please give me some solutions. 请给我一些解决方案。

Thanks in advance 提前致谢

To add fields to UserProfile in SimpleMembership (I will assume you want to use migrations and EF): 要将字段添加到SimpleMembership中的UserProfile中(我假设您要使用迁移和EF):

  1. Ensure you have enabled migrations (use the Enable-Migration in the package manager console) 确保已启用迁移(使用程序包管理器控制台中的Enable-Migration
  2. Edit the UserProfile class (assuming you are using, for example, the Visual Studio 2012 -> New Project -> ASP.NET MVC 4 Web Application -> Internet Application template, this will be created for you) to add your new properties (example below) 编辑UserProfile类(假设您使用的是Visual Studio 2012->新建项目-> ASP.NET MVC 4 Web应用程序-> Internet应用程序模板,将为您创建此模板)以添加新属性(示例下面)
  3. Add a migration which will update your database using the Add-Migration command. 添加一个迁移,该迁移将使用Add-Migration命令更新您的数据库。
  4. Optionally edit the generated migration to handle default values and non-nullable fields. (可选)编辑生成的迁移以处理默认值和不可为空的字段。
  5. Run that migration against your development database using the Update-Database command. 使用Update-Database命令对您的开发数据库运行该迁移。

I have provided an overview about how SimpleMembership works, with UserProfile in this answer . 我已经在此答案中使用UserProfile提供了有关SimpleMembership如何工作的概述。 Note that UserProfile can be moved from the UsersContext to the same DbContext as all your other classes, it does not have to be in a separate context. 请注意,可以将UserProfile与所有其他类一起从UsersContext移动到相同的DbContext,而不必在单独的上下文中。

SimpleMembership is designed to play well with code-first EF development, so that is the process I have outlined above. SimpleMembership旨在与代码优先EF开发一起很好地使用,所以这就是我上面概述的过程。 An example updated UserProfile class with a Forename , Surname and a LoginCount field would look like: 一个例子更新UserProfile类与ForenameSurnameLoginCount场会是什么样子:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }

    public string UserName { get; set; }

    [MaxLength(40)]
    [Required]
    public string Forename { get; set; }

    [MaxLength(40)]
    [Required]
    public string Surname { get; set; }

    [Required]
    public int LoginCount { get; set; }
}

References: 参考文献:

  1. ASP.NET MVC 4 Entity Framework Scaffolding and Migrations - task 3 shows how to enable migrations ASP.NET MVC 4实体框架脚手架和迁移 -任务3显示了如何启用迁移
  2. MSDN: Code First Migrations - everything you need to know about migrations MSDN:代码优先迁移 -您需要了解的有关迁移的所有信息
  3. StackOverflow: How do I use my own database with SimpleMembership and WebSecurity? StackOverflow:如何将自己的数据库与SimpleMembership和WebSecurity一起使用? What is MVC4 security all about? 什么是MVC4安全性?

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

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