6

我将 ASP.NET MVC 4 与 SimpleMemmbership 一起使用。

当我构建我的应用程序时,会自动构建以下表格

网页_会员

网页_OAuthMembership

网页_角色

我可以成功注册用户。

但是,webpages_UsersInRoles表似乎丢失了。

有谁知道为什么这张桌子不见了?

4

1 回答 1

8

本文可能会帮助您解决问题。

更新:

上面的文章让我们朝着正确的方向前进。我们的解决方案是在初始化期间使用的 UserProfile.cs 类中添加“webpages_UsersInRoles”的定义(我们首先编写代码)。

   [Table("UserProfile")]
public  class UserProfile
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [DataType(DataType.EmailAddress)]
    public string EmailAddress { get; set; }
    public bool IsEnabled { get; set; }
}



[Table("webpages_Membership")]
public class Membership
{
    public Membership()
    {
        //Roles = new List<Role>();
        OAuthMemberships = new List<OAuthMembership>();
        UsersInRoles = new List<UsersInRole>();
    }

    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int UserId { get; set; }
    public DateTime? CreateDate { get; set; }
    [StringLength(128)]
    public string ConfirmationToken { get; set; }
    public bool? IsConfirmed { get; set; }
    public DateTime? LastPasswordFailureDate { get; set; }
    public int PasswordFailuresSinceLastSuccess { get; set; }
    [Required, StringLength(128)]
    public string Password { get; set; }
    public DateTime? PasswordChangedDate { get; set; }
    [Required, StringLength(128)]
    public string PasswordSalt { get; set; }
    [StringLength(128)]
    public string PasswordVerificationToken { get; set; }
    public DateTime? PasswordVerificationTokenExpirationDate { get; set; }
    //public ICollection<Role> Roles { get; set; }

    [ForeignKey("UserId")]
    public ICollection<OAuthMembership> OAuthMemberships { get; set; }

    [ForeignKey("UserId")]
    public ICollection<UsersInRole> UsersInRoles { get; set; }
}

[Table("webpages_OAuthMembership")]
public class OAuthMembership
{
    [Key, Column(Order = 0), StringLength(30)]
    public string Provider { get; set; }

    [Key, Column(Order = 1), StringLength(100)]
    public string ProviderUserId { get; set; }

    public int UserId { get; set; }

    [Column("UserId"), InverseProperty("OAuthMemberships")]
    public Membership User { get; set; }
}

[Table("webpages_UsersInRoles")]
public class UsersInRole
{
    [Key, Column(Order = 0)]
    public int RoleId { get; set; }

    [Key, Column(Order = 1)]
    public int UserId { get; set; }

    [Column("RoleId"), InverseProperty("UsersInRoles")]
    public Role Roles { get; set; }

    [Column("UserId"), InverseProperty("UsersInRoles")]
    public Membership Members { get; set; }



}

[Table("webpages_Roles")]
public class Role
{
    public Role()
    {
        UsersInRoles = new List<UsersInRole>();
    }

    [Key]
    public int RoleId { get; set; }
    [StringLength(256)]
    public string RoleName { get; set; }

    //public ICollection<Membership> Members { get; set; }

    [ForeignKey("RoleId")]
    public ICollection<UsersInRole> UsersInRoles { get; set; }
}

然后在继承 dbContext 的类上,我们添加了 public DbSet UsersInRoles { get; 放; }。

  public class IntranetEntities : DbContext
{

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Membership> Memberships { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<UsersInRole> UsersInRoles { get; set; }

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

    public override int SaveChanges()
    {

        try
        {
            return base.SaveChanges();
        }
        catch (DbEntityValidationException dbEx)
        {
            foreach (var validationErrors in dbEx.EntityValidationErrors)
            {
                foreach (var validationError in validationErrors.ValidationErrors)
                {
                    Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                }
            }
        }
        return 0;

    }
}

然后我们继续构建我们的项目并使用我们执行的 VS 包管理器控制台(因为我们正在进行代码优先开发)

更新数据库-详细

网页_UsersInRoles

然后模型被更新到我们的期望。我已经提供了所有必需的代码,希望它能在未来的时间和悲伤中拯救某人。

于 2012-11-28T15:28:31.243 回答