1

每当我尝试重命名此问题中描述的表名时,我都会创建一个新的MVC 5项目

使用新的身份系统更改表名

它总是会破坏 Logins、Claims、Roles 表中的外键关系

我试图覆盖 OnModelCreating 方法,但这里是我的代码


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //base.OnModelCreating(modelBuilder);
        if (modelBuilder == null)
        {
            throw new ArgumentNullException("modelBuilder");
        }
        modelBuilder.Entity<App>().HasKey(m => new { m.AppId, m.FacebookId });
        modelBuilder.Entity<IdentityUser>().ToTable("Admins");
        modelBuilder.Entity<IdentityUser>().
            Property(p => p.Id).HasColumnName("AdminId");
        modelBuilder.Entity<Admin>()
            .ToTable("Admins")
            .Property(p => p.Id).HasColumnName("AdminId");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("Logins")
           .HasKey(m => new { m.ProviderKey, m.UserId, m.LoginProvider })
           .Property(m => m.UserId)
           .HasColumnName("AdminId");  
        modelBuilder.Entity<IdentityUserRole>().ToTable("AdminRoles")
            .HasKey(m => new { m.RoleId, m.UserId })
            .Property(m => m.RoleId)
            .HasColumnName("AdminRoleId");
        modelBuilder.Entity<IdentityUserRole>().Property(m => m.UserId)
            .HasColumnName("AdminId");
        //modelBuilder.Entity<IdentityUserRole>().HasRequired(m => m.UserId);
        //modelBuilder.Entity<IdentityUserLogin>().HasRequired(m => m.UserId);
        modelBuilder.Entity<IdentityUserClaim>().ToTable("Claims")
            .HasKey(m=> m.Id)
            .Property(m => m.Id)
            .HasColumnName("ClaimId");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles")
            .HasKey(m => m.Id).Property(m => m.Id).HasColumnName("RoleId");
    }

其中“Admin”类是我对 ApplicationUser Default 类的名称加上我自己的实现

public class Admin : IdentityUser
{
    [Required]
    public virtual List<App> Apps { get; set; }
    public bool? IsPremium { get; set; }
    [DataType(DataType.Date)]
    public DateTime? LastPublishDateTime { get; set; }
}

这是我的其他域类

public class App
{
    [Key]
    [Column("AppId", Order = 1)]
    public virtual int AppId { get; set; }
    [Required]
    [Key]
    [Column("FacebookId", Order = 2)]
    public virtual string FacebookId { get; set; }
    [Required]
    public virtual string Secret { get; set; }      
    public virtual List<User> Users { get; set; }
    public virtual List<Post> Posts { get; set; }
    [Required]
    public virtual Admin Admin { get; set; }
}


public class Post
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int PostId { get; set; }
    public virtual string Title { get; set; }
    public virtual string Content { get; set; }
    public virtual string Link { get; set; }
    public virtual string Image { get; set; }
    public virtual bool IsSpecial { get; set; }
    [Required]
    public virtual App App { get; set; }
    [Required]
    public virtual DateTime? PublishDate { get; set; }
}

public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int UserId { get; set; }
    [MaxLength(500)]
    public virtual string FacebookId { get; set; }
    [MaxLength(500)]
    public virtual string Token { get; set; }
    [Required]
    public virtual App App { get; set; }
}
4

0 回答 0