简体   繁体   English

每个类继承的实体框架表

[英]Entity Framework Table Per Class Inheritance

I am trying to implement a history table for an entity in EF6, code first. 我正在尝试先在EF6中为实体实现历史记录表。

I figured there would be a way to do this with inheritance. 我认为会有一种继承的方法。 The history table, which is a derived type of the actual table entity, just containing straight copies of all the properties. 历史表是实际表实体的派生类型,仅包含所有属性的直接副本。 Along with an edit to the key. 以及对密钥的编辑。

My code first table entity config for Booking . 我的代码为Booking第一个表实体配置。

public class BookingEntityConfiguration
    : EntityTypeConfiguration<Booking>
{
    public BookingEntityConfiguration()
    {
        Property(b => b.BookingId).HasColumnOrder(0);
        HasKey(b => new { b.BookingId });

        HasOptional(b => b.BookingType)
            .WithMany()
            .HasForeignKey(c => c.BookingTypeId);
    }
}

My code first table entity config for BookingHistory . 我的代码第一个表实体配置为BookingHistory

public class BookingHistoryTypeEntityConfiguration
    : EntityTypeConfiguration<BookingHistory>
{
    public BookingHistoryTypeEntityConfiguration()
    {
        Property(b => b.BookingId).HasColumnOrder(0);
        Property(b => b.BookingVersion).HasColumnOrder(0);
        HasKey(b => new { b.BookingId, b.BookingVersion });
    }
}

Where 哪里

public class BookingHistory : Booking { }

My BookingHistory table never gets generated in the contexts associated database, which includes these references to the table entities: 我的BookingHistory表从未在关联上下文的数据库中生成,该数据库包括对表实体的以下引用:

public DbSet<Booking> Bookings { get; set; }
public DbSet<BookingHistory> BookingHistories { get; set; }

Is there any simple way to achieve what I want? 有什么简单的方法可以实现我想要的吗? Which is the derived entity (history table) generates a table that contains the same column fields as the base class entity, but with a change of key. 哪个是派生实体(历史表)会生成一个表,该表包含与基类实体相同的列字段,但键有所变化。

I appreciate my code above is pretty naive, but I can't seem to find a blog post of similar to help. 我很欣赏上面的代码很幼稚,但是我似乎找不到类似的博客文章来提供帮助。

The best way is to have a base type from which both the entity and its history entity inherit: 最好的方法是具有一个基本类型,实体及其历史实体都将从该基本类型继承:

public class BookingsContext : DbContext
{

    public DbSet<Booking> Bookings { get; set; }
    public DbSet<BookingHistory> BookingHistories { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BookingBase>()
            .HasKey(p => p.BookingId)
            .Property(p => p.BookingId)
                       .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        modelBuilder.Entity<Booking>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("Booking");
            });

        modelBuilder.Entity<BookingHistory>().Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("BookingHistory");
            });
    }
}

By ToTable you specify that both entities should be mapped to different tables. 通过ToTable您可以指定两个实体都应映射到不同的表。 On top of that, MapInheritedProperties tells EF to mapp all properties from the base type to this table as well. 最重要的是, MapInheritedProperties告诉EF也将所有属性从基本类型映射到该表。 the result is two completely independent tables that can be addressed by two separate DbSet properties. 结果是两个完全独立的表,可以通过两个单独的DbSet属性对其进行寻址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架中的继承-每个具体类的表? - Inheritance in entity framework - Table per concrete class? 每个层次结构继承的实体框架表 - Entity Framework Table per Hierarchy Inheritance 实体框架7中的按层次结构表继承 - Table-per-Hierarchy inheritance in Entity Framework 7 实体框架6中的每个具体类型的表(TPC)继承(EF6) - Table Per Concrete Type (TPC) Inheritance in Entity Framework 6 (EF6) 根据类型继承手动设置实体框架表 - Manually set up Entity Framework's Table per type inheritance 实体框架中的级联删除(每类继承的表) - Cascade delete in entity framework ( table per type inheritance ) 实体框架4:插入时每个类型的表继承问题 - Entity Framework 4: Table per Type inheritance problem on insert 实体框架6个自定义键,用于每种类型的表继承 - Entity framework 6 custom keys for table-per-type inheritance 实体框架“每个具体类的表”映射到错误的表 - Entity framework 'table-per-concrete-class' maps to wrong table 实体框架+ mvc +代码优先+每个层次结构的继承表,如何使用传统/多态性避免切换/如果 - Entity framework + mvc + code first + Inheritance table per Hierarchy, How to use heritage/polymorphism to avoid switch/if
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM