每个类继承的实体框架表 [英] Entity Framework Table Per Class Inheritance

查看:94
本文介绍了每个类继承的实体框架表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为EF6中的实体实现历史记录表,首先进行编码.

I am trying to implement a history table for an entity in EF6, code first.

我认为会有一种通过继承来做到这一点的方法.历史表是实际表实体的派生类型,仅包含所有属性的直接副本.以及对键的编辑.

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.

我的代码Booking的第一个表实体配置.

My code first table entity config for 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);
    }
}

我的代码BookingHistory的第一个表实体配置.

My code first table entity config for 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 });
    }
}

哪里

public class BookingHistory : Booking { }

我的BookingHistory表从不与上下文关联的数据库生成,该数据库包括对表实体的这些引用:

My BookingHistory table never gets generated in the contexts associated database, which includes these references to the table entities:

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");
            });
    }
}

通过ToTable,您可以指定两个实体都应映射到不同的表.最重要的是,MapInheritedProperties告诉EF也将所有属性从基本类型映射到该表.结果是两个完全独立的表,可以通过两个单独的DbSet属性对其进行寻址.

By ToTable you specify that both entities should be mapped to different tables. On top of that, MapInheritedProperties tells EF to mapp all properties from the base type to this table as well. the result is two completely independent tables that can be addressed by two separate DbSet properties.

这篇关于每个类继承的实体框架表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆