webpages_UsersInRoles missing

后端 未结 1 1906
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 08:19

I am using ASP.NET MVC 4 together with SimpleMemmbership.

When I built my application the following tables were built automatically

webpages_Membership

相关标签:
1条回答
  • 2020-12-19 08:34

    This article might assist you in resolving your issue.

    Update:

    The article above got us going in the right direction. Our solution was to add a definition for "webpages_UsersInRoles to our UserProfile.cs class that gets used during initialization (we are doing code first).

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

    Then on the class that inherits dbContext we added the public DbSet UsersInRoles { get; set; }.

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

    We then proceeded to build our project and using the VS package manager console we executed (since we are doing code first development)

    update-database -verbose

    webpages_UsersInRoles

    The model was then updated to our expectations. I have provided all the required code in the hopes it saves someone in the future time and grief.

    0 讨论(0)
提交回复
热议问题