asp.net-mvc – 缺少webpages_UsersInRoles

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 缺少webpages_UsersInRoles前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用ASP.NET MVC 4和SimpleMemmbership.

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

webpages_Membership

webpages_OAuthMembership

webpages_Roles

我可以成功注册用户.

但是,webpages_UsersInRoles表似乎丢失了.

有谁知道为什么这张桌子丢失了?

解决方法

This article可能会帮助您解决问题.

更新:

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

[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的类上添加了公共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包管理器控制台(因为我们正在进行代码优先开发)

update-database -verbose

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

猜你在找的asp.Net相关文章