[英]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.