繁体   English   中英

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

[英]Table Per Type Fluent Mapping in Entity Framework

使用Entity Framework,我可以将相关表映射为类继承。有三种不同的方法来表示继承层次结构(通过weblogs ):

  • 每个层次表(TPH)
  • 每种类型的表(TPT)
  • 每个混凝土等级表(TPC)

网站mscblogs对这些方法中的每一种都有一个很好的解释。

我试图了解如何使用方法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");
        // ...
    }    
}

// ...

我正在寻找几个小时,但我找不到任何东西。 我发现了许多如何用图表属性其他方法做到这一点的例子,但没有流畅的api。

如何在Entity Framework 4.1 Fluent API中映射TPT?

映射每个类型的表(TPT)继承

在TPT映射方案中,所有类型都映射到各个表。 仅属于基本类型或派生类型的属性存储在映射到该类型的表中。 映射到派生类型的表还存储将派生表与基表连接的外键。

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

资源

检查之前的问题的答案 ,希望它有所帮助。

更新完整示例

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