使用EF Code First的自定义表的SimpleMembership

时间:2013-08-25 03:11:10

标签: c# entity-framework asp.net-mvc-4 code-first simplemembership

我正在关注pluralsight的HTML5业务线课程以使用简单会员资格。我使用相同的代码,但使用SimpleMembership创建帐户与自定义表时出错。我无法找到它的错误。有人可以帮忙吗?

  

无效的列名称'ConfirmationToken'。
无效的列名称   'PasswordChangedDate'。
无效的列名称   'PasswordFailuresSinceLastSuccess'。

这是我的代码

注册方法:

  public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
                        new
                        {
                            FirstName = "Admin",
                            LastName = "Admin",
                            IsActive = true,
                            CreatedOn = DateTime.Now,
                            ModifiedOn = DateTime.Now
                        });
                    WebSecurity.Login(model.UserName, model.Password);
                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

实体:

 public class Membership 
    {
        public int UserId {get; set;}
        public DateTime? CreateDate { get; set; }
        public string ConfirmationTokeen { get; set; }
        public bool? IsConfirmed { get; set; }
        public DateTime? LastPaswordFailureDate { get; set; }
        public int PasswordFailureSinceLastSuccess { get; set; }
        public string Password { get; set; }
        public DateTime? PasswordChangeDate { get; set; }
        public string PasswordSalt { get; set; }
        public string PasswordVerificationToken { get; set; }
        public DateTime? PasswordVerificationTokenExpirationDate { get; set; }
    }

public class OAuthMembership 
    {
        public string  Provider { get; set; }
        public string ProviderUserId { get; set; }
        public int UserId { get; set; }
    }
public class Role
    {
        public int RoleId {get; set;}
        public string RoleName {get; set;}

        public ICollection<User> Users { get; set; }

        public Role()
        {
            this.Users = new List<User>();
        }
    }
  public class User : IAuditInfo
    {
        public int Id {get; set;}

        public string Username{get; set;}

        public string FirstName {get; set;}
        public string LastName { get; set; }


        public bool IsActive{get; set;}

        public DateTime? CreatedOn { get; set; }
        public DateTime? ModifiedOn {get; set;}

         public ICollection<Role> Roles { get; set; }

        public User()
        {
            this.Roles = new List<Role>();
        }
    }

配置

 public UserConfiguration()
        {
            this.Property(p => p.Id).HasColumnOrder(0);
            this.Property(p => p.Username).IsRequired().HasMaxLength(200);
            this.Property(p => p.FirstName).IsOptional().HasMaxLength(100);
            this.Property(p => p.LastName).IsOptional().HasMaxLength(100);

            this.HasMany(a => a.Roles).WithMany(b => b.Users).Map(m =>
            {
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");
                m.ToTable("webpages_UsersInRoles");
            });
        }

 public RoleConfiguration()
        {
            this.ToTable("webpages_Roles");
            this.Property(p => p.RoleName).HasMaxLength(256).HasColumnType("nvarchar").IsRequired();
        }

 public OAuthMembershipConfiguration()
        {
            this.ToTable("webpages_OAuthMembership");
            this.HasKey(k => new { k.Provider, k.ProviderUserId });
            this.Property(p => p.Provider).HasMaxLength(128).HasColumnType("nvarchar").IsRequired();
            this.Property(p => p.ProviderUserId).HasMaxLength(128).HasColumnType("nvarchar").IsRequired();
            this.Property(p => p.UserId).IsRequired();
        }

public MembershipConfiguration()
        {
            this.ToTable("webpages_Membership");
            this.HasKey(s => s.UserId);
            this.Property(p => p.UserId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            this.Property(p => p.ConfirmationTokeen).HasMaxLength(128).HasColumnType("nvarchar");
            this.Property(p => p.PasswordFailureSinceLastSuccess).IsRequired();
            this.Property(p => p.Password).IsRequired().HasMaxLength(128).HasColumnType("nvarchar");
            this.Property(p => p.PasswordSalt).IsRequired().HasMaxLength(128).HasColumnType("nvarchar");
            this.Property(p => p.PasswordVerificationToken).HasMaxLength(128).HasColumnType("nvarchar");
        }

过滤:

  

public SimpleMembershipInitializer()               {

            DataContext context = new DataContext();
            context.Database.Initialize(true);

            try
            {
                    if (!context.Database.Exists())
                    {
                        ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                    }
                WebSecurity.InitializeDatabaseConnection(
                    Config.ConnectionStringName, 
                    Config.UsersTableName, 
                    Config.UsersPrimaryKeyColumnName, 
                    Config.UsersUsernameColumnName, 
                    autoCreateTables: true);
            }
            catch (Exception ex)
            {
            }
        }

0 个答案:

没有答案