1

我有错误:“错误 0114:关系约束中从属角色和主要角色中的属性数量必须完全相同。” 在这段代码上

  public abstract class UserBase
    {
        [Key]
        public Guid Guid { get; set; }     

        [Required]
        public string Name { get; set; }   
    }

    public class User : UserBase
    {
        public DateTime? BirthDate { get; set; }    
        public UserPassword Password { get; set; }        
    }

    public class Outsourcer : UserBase
    {
        public Guid OutsourcingCompanyGuid { get; set; }    
        public OutsourcingCompany OutsourcingCompany { get; set; }
    }

    public class UserPassword
    {
            [Key]
            public Guid UserGuid { get; set; }
            public User User { get; set; }

            public string Password { get; set; }
            public string Salt { get; set; }
    }

    public class MyContext : DbContext
    {
        public DbSet<UserBase> Users { get; set; }
        public DbSet<UserPassword> UserPasswords { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<UserBase>().ToTable("UserBase");
            modelBuilder.Entity<Outsourcer>().ToTable("Outsourcer");

            modelBuilder.Entity<User>().ToTable("User");
            modelBuilder.Entity<User>().HasRequired(t => t.Password).WithRequiredPrincipal(t => t.User);
            modelBuilder.Entity<UserPassword>().ToTable("User");
        }
}

我想将用户和用户密码合并到一个名为“用户”的表中。没有行的代码modelBuilder.Entity<UserPassword>().ToTable("User");可以工作,但我得到了两个不同的表。

4

0 回答 0