Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Keep Entity Framework Code First from using Derived Classes that Aren't Part of the Model

Here is a generic example of what I'm doing, what is happening, and what SHOULD be happening:

First, I declare my code first model, something like this:

Context:

namespace Models {
    public class MyContext : DbContext
    {
        public DbSet<ClassA> ClassA { get; set; }
        public DbSet<ClassB> ClassB { get; set; }

        public MyContext()
            : base("name=MyContext")
        {
        }
    }
}

Classes A and B:

namespace Models {
    public class ClassA
    {
        public int ClassAID { get; set; }
        public string BaseAVal { get; set; }
        public virtual ICollection<ClassB> ClassBChildren {get; set; }//Relation Property
    }
    public class ClassB
    {
        public int ClassBID { get; set; }
        public int ClassAID { get; set; }//Foreign Key
        public string BaseBVal { get; set; }
        public virtual ClassA ClassAParent { get; set; }//Relation Property
    }
}

Now elsewhere, in another namespace, I have classes for a different purpose that derive from the model classes.

Derived Classes:

namespace CalculatedModels {
    public class DerivedClassA : ClassA
    {
        string SecondAVal { get; set; }
        public static DerivedClassA fromDBItem(ClassA dbItem) {
            //Create a DerivedClassA
            //Copy over inherited properties from dbItem (via Reflection)
            //Calculate its non-inherited properties
            //return it
        }
        public ClassA toDBItem() {
            //Create a ClassA
            //Copy over inherited properties from this (via Reflection)
            //return it
        }
    }
    public class DerivedClassB : ClassB
    {
        string SecondBVal { get; set; }
        //Conversion functions similar to those in DerivedClassA go here...
    }
}

The problem arises when I have EntityFramework generate the database from this model. I DON'T want it to include ANYTHING from the derived classes. They are NOT for the database to concern itself with! Why else would I put them in a completely different namespace and make the collections in the context be of the non-extended base types? Tables SHOULD look like this:

Table: ClassA
    ---Columns---
    ClassAID (PrimaryKey, int, not null)
    BaseAVal (nvarchar(max), null)

Table: ClassB
    ---Columns---
    ClassBID (PrimaryKey, int, not null)
    ClassAID (ForeignKey, int, not null)
    BaseBVal (nvarchar(max), null)

But EF insists on ruining my intended schema by adding the newly declared properties of the the derived classes like so:

Table: ClassA
    ---Columns---
    ClassAID (PrimaryKey, int, not null)
    BaseAVal (nvarchar(max), null)
    SecondAVal (nvarchar(max), null) --This should NOT be here!!
    Discriminator (nvarchar(128)) --Neither should this!!

Table: ClassB
    ---Columns---
    ClassBID (PrimaryKey, int, not null)
    ClassAID (ForeignKey, int, not null)
    BaseBVal (nvarchar(max), null)
    SecondBVal (nvarchar(max), null) --This should NOT be here!!
    Discriminator (nvarchar(128)) --Neither should this!!

As you can see, it is including the derived classes as a possible part of the model and/or database system, despite NOTHING in the model having ANYTHING to do with them. THEY reference stuff in the model, NOT the other way around--EVER. I DON'T want it to use them! How do I keep it from doing this?

like image 826
FireWingLead Avatar asked Oct 27 '25 07:10

FireWingLead


1 Answers

Entity framework is doing what it should do here as your setup follows the Table-Per-Hierarchy (TPH) Inheritance pattern.

Discriminator is added so that database can distinguish when a ClassA or a DervivedClassA is added to the DB

You can stop EF mapping your Derived classes using NotMapped attribute. add the following namespace

using System.ComponentModel.DataAnnotations.Schema;

DerivedClassB

[NotMapped]
public class DerivedClassB : ClassB
{
    string SecondBVal { get; set; }
    //Conversion functions similar to those in DerivedClassA go here...
}
like image 152
SWilko Avatar answered Oct 28 '25 20:10

SWilko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!