如何在 MVC4 中的 UserProfile 中创建自定义附加字段 [英] How to create custom additional fields in UserProfile in MVC4

查看:14
本文介绍了如何在 MVC4 中的 UserProfile 中创建自定义附加字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了新的 ASP MVC 4 特性,它附带了新的成员数据库架构和新的初始化.在 mvc 3 和旧版本中,开发人员能够使用 web.config 中的规范创建自定义用户配置文件字段,但现在我在默认 mvc 4 项目中面临过滤器命名空间中的方法:

I faced with new ASP MVC 4 feature, it shipped with new membership db schema and new initialization. In mvc 3 and old versions developer able to create custom user profile fields using specifications in web.config, but now i faced with method in filters namespace in default mvc 4 project:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

和用户资料表:

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

但是方法 InitializeDatabaseConnection 只生成 UserName 和 UserId 我需要生成其他附加字段.

But the method InitializeDatabaseConnection generate only UserName and UserId i need to generate other additional fields.

我在 EF codeFirst 方法方面有很好的经验,在这种情况下,我尝试编辑 UserProfile 类:

I have good experience in EF codeFirst approach, and in that case i try to edit UserProfile Class:

[Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        [Column]
        [Required]
        public string UserName { get; set; }
        [Column]
        [Required]
        public string FirstName { get; set; }
        [Column]
        [Required]
        public string LastName { get; set; }
    }

但是当我重新生成数据库时,我没有看到任何更改,未生成自定义 Db 字段.请帮助我,我如何创建自定义用户字段?

But when i regenerate database, i havent see any changes, Custom Db fields not generated. Help me please, how can i create custom user fields?

推荐答案

从上面的答案中细化

WebSecurity.InitializeDatabaseConnection 方法帮助说明

如果要使用包含用户配置文件信息(用户名、电子邮件地址等)的数据库表,请指定成员资格系统用于连接到该信息的连接字符串和表名.如果不想使用现有的用户配置文件表,则可以指定 InitializeDatabaseConnection() 方法应自动创建用户配置文件表.(用户配置文件表的数据库必须已经存在.)

If you want to use a database table that contains user profile information (user names, email addresses, and so on), you specify a connection string and table name that the membership system uses to connect to that information. If you do not want to use an existing user profile table, you can specify that the InitializeDatabaseConnection() method should automatically create the user profile table. (A database for the user profile table must already exist.)

因此,如果我们想要在 UserProfile 表中添加更多字段,我们只需要确保我们正在创建配置文件表并在该表已经存在后运行 InitializeDatabaseConnection 方法地点.

So if we want more fields into the UserProfile table we just need to make sure we are creating a profile table and run the InitializeDatabaseConnection method after the table is already in place.

在来自 VS2012 的标准 MVC4.0 项目模板中,我已经注释掉了帐户控制器

In the standard MVC4.0 project template from VS2012 I've commented out the Account controller

[Authorize]
//[InitializeSimpleMembership]
public class AccountController : Controller
{

并将 InitializeDatabaseConnection 移动到 EF Code First Database Initializer

and moved InitializeDatabaseConnection into the EF Code First Database Initializer

public class MyDatabaseInit: DropCreateDatabaseAlways<MyDatabaseContext>
{
    protected override void Seed(MyDatabaseContext context)
    {
        SeedMembership();
    }

    private void SeedMembership()
    {
        WebSecurity.InitializeDatabaseConnection("MyDatabaseContext",
            "UserProfile", "UserId", "UserName", autoCreateTables: true);
    } 
}

确保 InitializeDatabaseConnection 在表已经就位后运行.

ensuring that the InitializeDatabaseConnection runs once the table is already in place.

UserProfile 类添加到我的 EF Code First 模型

Added the UserProfile class to my EF Code First model

public class MyDatabaseContext : DbContext
{
    public DbSet<UserProfile> UserProfiles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

UserProfile 表中添加了额外的字段

Added the extra field in the UserProfile table

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

您现在所需要做的就是在应用程序启动时设置数据库初始化策略,并在调用任何授权/身份验证代码之前调用数据库查询以确保在该点创建它.

All you need now is to set the database initialization strategy when the application starts and also call a query on the database the make sure it gets created at that point, before any authorization/authentication code is called.

protected void Application_Start()
{
   AreaRegistration.RegisterAllAreas();

   WebApiConfig.Register(GlobalConfiguration.Configuration);
   FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
   RouteConfig.RegisterRoutes(RouteTable.Routes);
   BundleConfig.RegisterBundles(BundleTable.Bundles);
   AuthConfig.RegisterAuth();

   Database.SetInitializer<MyDatabaseContext>(new MyDatabaseInit());
   new MyDatabaseContext().UserProfile.Find(1);
}

这篇关于如何在 MVC4 中的 UserProfile 中创建自定义附加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆