简体   繁体   English

实体框架中的每种类型Fluent映射表

[英]Table Per Type Fluent Mapping in Entity Framework

With Entity Framework I can map related tables as a class inheritance and There are three different approaches to representing an inheritance hierarchy (by weblogs ): 使用Entity Framework,我可以将相关表映射为类继承。有三种不同的方法来表示继承层次结构(通过weblogs ):

  • Table per Hierarchy (TPH) 每个层次表(TPH)
  • Table per Type (TPT) 每种类型的表(TPT)
  • Table per Concrete class (TPC) 每个混凝土等级表(TPC)

The site mscblogs has a nice explanation for each one of these approaches. 网站mscblogs对这些方法中的每一种都有一个很好的解释。

I'm trying to understand how to map my tables using the approach TPT (Table per Type), but unlike the example of mscblogs , I need to do the mapping for fluent programming like: 我试图了解如何使用方法TPT(每种类型的表)来映射我的表,但与mscblogs的示例不同,我需要为流畅的编程执行映射,如:

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;

public class BillingDetailMap : EntityTypeConfiguration<BillingDetailEntity>
{
    public BillingDetailMap()
    {
        // ...
        this.Property(t => t.Number).HasColumnName("Number");
        // ...
    }    
}

// ...

I'm searching for several hours but I couldn't find anything. 我正在寻找几个小时,但我找不到任何东西。 I found many examples how to do this with diagram , with attributes and others , but nothing with fluent api. 我发现了许多如何用图表属性其他方法做到这一点的例子,但没有流畅的api。

How to Mapping TPT in Entity Framework 4.1 Fluent API? 如何在Entity Framework 4.1 Fluent API中映射TPT?

Mapping the Table-Per-Type (TPT) Inheritance 映射每个类型的表(TPT)继承

In the TPT mapping scenario, all types are mapped to individual tables. 在TPT映射方案中,所有类型都映射到各个表。 Properties that belong solely to a base type or derived type are stored in a table that maps to that type. 仅属于基本类型或派生类型的属性存储在映射到该类型的表中。 Tables that map to derived types also store a foreign key that joins the derived table with the base table. 映射到派生类型的表还存储将派生表与基表连接的外键。

modelBuilder.Entity<Course>().ToTable("Course");  
modelBuilder.Entity<OnsiteCourse>().ToTable("OnsiteCourse");

Source 资源

Check also my answer on previous question, hopefully it helps. 检查之前的问题的答案 ,希望它有所帮助。

update complete example 更新完整示例

public class AppContext : DbContext
{
    public DbSet<Item> Items { get; set; } // --> this dbset is required for TPT, if removed it will become TPCC
    public DbSet<Food> Books { get; set; }
    public DbSet<Book> Foods { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ItemMap());
        modelBuilder.Configurations.Add(new BookMap());
    }
}
public class ItemMap : EntityTypeConfiguration<Food>
{
    public ItemMap()
    {
        ToTable("Foods");
    }
}
public class BookMap : EntityTypeConfiguration<Book>
{
    public BookMap()
    {
        ToTable("Books");
    }
}
public abstract class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class Food : Item { }
public class Book : Item { }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM