SimpleMemership CreateUserAndAccount定制 [英] SimpleMemership CreateUserAndAccount Customization

查看:240
本文介绍了SimpleMemership CreateUserAndAccount定制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个新的属性在我的模型添加到用户配置

I am trying to add a new property to the UserProfile class in my model

public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email Address")]
    public string Email { get; set; } \\this is the new property
    public virtual IList<Game> Games { get; set; }
}

我想将它添加到我的种子方法我 Configurations.cs 文件

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

        var roles = (SimpleRoleProvider)Roles.Provider;
        var membership = (SimpleMembershipProvider)Membership.Provider;

        if (!roles.RoleExists("Administrator"))
        {
            roles.CreateRole("Administrator");
        }
        if (membership.GetUser("drew", false) == null)
        {

            membership.CreateUserAndAccount(
                "drew", 
                "password", false,
                new { Email = "myemail@gmail.com" }); \\ this line errors
        }
        if (!roles.GetRolesForUser("drew").Contains("Administrator"))
        {
            roles.AddUsersToRoles(new[] { "drew" }, new[] { "Administrator" });
        }
    }

不过,我收到一个错误:

However, I receive an error:

错误2参数4:无法从AnonymousType#1'转换'System.Collections.Generic.IDictionary&LT;字符串对象&gt;' C:\\ code \\黑手党\\ Mafia.EF \\迁移\\ Configuration.cs 65 21 Mafia.EF

我跟随的例子,如:<一href=\"http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-$c$cfirst-and-custom-user-properties/\">http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-$c$cfirst-and-custom-user-properties/

但我想出什么...我的 WebMatrix.WebData 版本 v4.0.30319

But I'm coming up with nothing... My WebMatrix.WebData version is v4.0.30319

有什么想法?

推荐答案

尝试这样的:

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

    if (!Roles.RoleExists("Administrator"))
    {
        Roles.CreateRole("Administrator");
    }

    if (!WebSecurity.UserExists("drew"))
    {
        WebSecurity.CreateUserAndAccount(
            "drew", 
            "password",
            new { Email = "myemail@gmail.com" },
            false
        );
    }

    if (!Roles.GetRolesForUser("drew").Contains("Administrator"))
    {
        Roles.AddUsersToRoles(new[] { "drew" }, new[] { "Administrator" });
    }
}

下面是一个<一个href=\"http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-$c$cfirst-and-custom-user-properties/\"><$c$c>nice教程 有关SimpleMembershipProvider你可能会考虑经历。

Here's a nice tutorial about the SimpleMembershipProvider you might consider going through.

这篇关于SimpleMemership CreateUserAndAccount定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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