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

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

问题描述

我面临着新的ASP MVC 4的功能,它附带了新的会员DB模式和新的初始化。在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我需要生成其他额外的字段只生成用户名和用户ID。

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

我在EF codeFirst做法好经验,在这种情况下,我尝试编辑用户配置类:

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; }
    }

但是,当我重​​新生成数据库,我还没有看到任何变化,而不是生成的自定义数据库字段。
请帮我,我怎么可以创建自定义用户字段?

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()方法会自动创建用户档案表。 (A为用户配置表数据库必须已经存在。)

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.)

因此​​,如果我们想要更多的领域进入用户配置表中,我们只需要确保我们正在创建一个配置文件表并运行 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第一个数据库初始化程序

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 运行一次表已经到位。

新增了用户配置类,我的EF code首先模型

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>();
    }
}

添加额外的字段中用户配置

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

你现在需要的是设置数据库初始化策略时,应用程序启动并还呼吁对数据库的查询确保它被创建在这一点上,任何授权/认证code调用之前。

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在用户配置自定义的附加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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