Asp.net 我可以使用EF 6.1和SQL Server 2012将SQL表拆分为三个类吗?

Asp.net 我可以使用EF 6.1和SQL Server 2012将SQL表拆分为三个类吗?,asp.net,asp.net-mvc,entity-framework,Asp.net,Asp.net Mvc,Entity Framework,我有下面的类,我为这个例子简化了它。该类有一个id 还有一些和房子有关的房产,还有一些和房间有关的房产。所有数据都已存储 在SQL表的一行中 namespace ClassLibrary1.Models { public partial class AspNetUser { public string Id { get; set; } public Nullable<int> AdminHouseNumber { get; set; }

我有下面的类,我为这个例子简化了它。该类有一个id 还有一些和房子有关的房产,还有一些和房间有关的房产。所有数据都已存储 在SQL表的一行中

namespace ClassLibrary1.Models
{
    public partial class AspNetUser
    {
        public string Id { get; set; }
        public Nullable<int> AdminHouseNumber { get; set; }
        public string AdminHouseCity { get; set; }
        public Nullable<int> AdminRoomNumber { get; set; }
        public string AdminRoomFloor { get; set; }
    }
}
这将映射到我的数据库,如下所示:

namespace ClassLibrary1.Models.Mapping
{
    public class AspNetUserMap : EntityTypeConfiguration<AspNetUser>
    {
        public AspNetUserMap()
        {
            // Primary Key
            this.HasKey(t => t.Id);

            // Properties
            this.Property(t => t.Id)
                .IsRequired()
                .HasMaxLength(128);

            // Table & Column Mappings
            this.ToTable("AspNetUsers");
            this.Property(t => t.Id).HasColumnName("Id");
            this.Property(t => t.AdminHouseNumber).HasColumnName("AdminHouseNumber");
            this.Property(t => t.AdminHouseCity).HasColumnName("AdminHouseCity");
            this.Property(t => t.AdminRoomNumber).HasColumnName("AdminRoomNumber");
            this.Property(t => t.AdminRoomFloor).HasColumnName("AdminRoomFloor");
        }
    }
}
出于性能原因,我希望将所有数据保存在一个表中。然而 我可以创建另外两个类。一个住房子,一个住房间 三个类都链接到同一个表吗

namespace ClassLibrary1.Models
{
    public partial class AspNetUser
    {
        public string Id { get; set; }
        public string RoomId { get; set; }
        public string HouseId { get; set; }
        public virtual House { get; set; }
        public virtual Room { get; set; }

       public class House
           public string HouseId { get; set; }
           public Nullable<int> AdminHouseNumber { get; set; }
           public string AdminHouseCity { get; set; }
       }

       Public class Room
           public string RoomId { get; set; }
           public Nullable<int> AdminRoomNumber { get; set; }
           public string AdminRoomFloor { get; set; }
       }

    }
}

你要找的似乎就是所谓的ComplexTypes支持,一些谷歌搜索显示

我理解这个问题,因为你有三种类型的AspNetUser;用户、房屋和房间。如果我错了,那么这个答案就没有意义了——请让我知道

您可能希望在建模中使用表/层次继承,但这将需要表中的另一个标志列才能工作,例如,包含0表示AspNetUser,1表示House,2表示Room。我发现使用TPH时性能稍微慢一点,但基本上是这样做的

public class AspNetUserBase
{
    //Define columns shared between all here
}

public class AspNetUser: AspNetUserBase
{
    // Define columns only for AspNetUser here
}

public class House: AspNetUserBase
{
    // Define columns only for House here
}

// Repeat for Room

// Now define the "split" in OnModelCreating
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // Any code you need etc.

    // This part maps all three to the table
    modelBuilder.Entity<AspNetUserBase>()
      .Map<AspNetUser>(m => m.Requires("Your Flag Column").HasValue(0)) // 0 is example
      .Map<House>(m => m.Requires("Your Flag Column").HasValue(1)) // 1 is example
      .Map<Room>(m => m.Requires("Your Flag Column").HasValue(2)) // 2 is example
}
然后,您就可以在AspNetUser、Home或Room上直接执行CRUD,而无需引用基类

然而,我不得不质疑,出于性能原因,他们是否需要全部坐在同一张桌子上,因为我的经验几乎总是证明了这一点。这很难具体,因为我当然不知道你的整个模型,但我敦促你在某个时候重新评估

public class AspNetUserBase
{
    //Define columns shared between all here
}

public class AspNetUser: AspNetUserBase
{
    // Define columns only for AspNetUser here
}

public class House: AspNetUserBase
{
    // Define columns only for House here
}

// Repeat for Room

// Now define the "split" in OnModelCreating
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // Any code you need etc.

    // This part maps all three to the table
    modelBuilder.Entity<AspNetUserBase>()
      .Map<AspNetUser>(m => m.Requires("Your Flag Column").HasValue(0)) // 0 is example
      .Map<House>(m => m.Requires("Your Flag Column").HasValue(1)) // 1 is example
      .Map<Room>(m => m.Requires("Your Flag Column").HasValue(2)) // 2 is example
}