Table Per Concrete Type (TPC) Inheritance in Entity Framework 6 (EF6)

后端 未结 2 2139
自闭症患者
自闭症患者 2021-02-20 05:39

In an effort to avoid the use of Table Per Hierarchy (TPH) I have been looking at examples of how best to implement Table-Per-Concrete Class (TPC) inheritance in my database mod

2条回答
  •  醉话见心
    2021-02-20 06:08

    Just to make this all simpler, I've moved the code necessary to force TablePerConcrete to open source. Its purpose is to allow features normally only available in the Fluent Interface (where you have to scatter a lot of code into your Db class' OnModelCreating method) to migrate over to Attribute-based features.

    It allows you to do things like this:

    [TablePerConcrete]
    public class MySubclassTable : MyParentClassEntity
    

    Forcing TPC regardless of what EF might decide to infer from your parent class/subclass relationship.

    One interesting challenge here is that sometimes EF will screw up an inherited Id property, setting it to be filled with an explicit value rather than being database-generated. You can ensure it doesn't do that by having the parent class implement interface IId (which just says: This has an Id property), then marking the subclasses with [ForcePKId].

    public class MyParentClassEntity : IId
    {
        public int Id { get; set; }
        . . .
    
    [TablePerConcrete]
    [ForcePKId]
    public class MySubclassTable : MyParentClassEntity
    {
        // No need for  PK/Id property here, it was inherited and will work as
        // you intended.
    

    Kicking off the code that handles all this for you is pretty simple - just add a couple lines to your Db class:

    public class Db : DbContext
    {
        . . .
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var modelsProject = Assembly.GetExecutingAssembly();
            B9DbExtender.New().Extend(modelBuilder, modelsProject);
    

    You can access it one of 2 ways:

    1. Via a single gist with all the relevant classes copy-pasted into a single file, here: https://gist.github.com/b9chris/8efd30687d554d1ceeb3fee359c179f9

    2. Via a library, our Brass9.Data, which we're releasing open source. It has a lot of other EF6 tools in it, like Data Migrations. It's also more organized, with classes broken out into separate files as you'd normally expect: https://github.com/b9chris/Brass9.Data

提交回复
热议问题