Entity Framework 5 Code-First Many-to-Many Table Inherited Class Reassignment (TPC)

Good. I am having some problems with the Entity framework when trying to specify the name of the join tables like this:

// Used by a bunch of things...
public abstract BaseClass
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public abstract ThingsBaseClass : BaseClass
{
    public virtual ICollection<Things> Things { get; set; }    
}

public First : ThingsBaseClass
{
    // This holds items of type First and doesn't have any other properties
}

public Second : ThingsBaseClass
{
    // This holds items of type Second and doesn't have any other properties
}

public Things
{
    public int Id { get; set; }
    public string Description { get; set; }

    // Many to Many
    public virtual ICollection<First> Firsts { get; set; }
    public virtual ICollection<Second> Seconds { get; set; }
}

      

So everything works fine except for the following tables:

First
FirstThings
Second
SecondThings
Things

      

I am trying to rename to:

First
Second
ThingFirsts
ThingSeconds
Things

      

Trying to rename them using the following code gives some very strange and random errors:

public class ThingConfiguration : EntityTypeConfiguration<Thing>
{
    HasMany(x => x.Firsts)
        .WithMany(x => x.Things)
         .Map(x => x.ToTable("ThingFirsts"));

    HasMany(x => x.Firsts)
        .WithMany(x => x.Things)
        .Map(x => x.ToTable("ThingSeconds"));    
}

      

I am trying to use First First Migrations to update a database (or just create one from scratch)

Errors include some of the following:

Schema specified is not valid. Errors: (28,6) : error 0040: Type Thing_First is not defined in namespace Project.Domain.Repositories (Alias=Self).

or

Schema specified is not valid. Errors: (126,6) : error 0074: NavigationProperty 'Thing' is not valid. Type 'Project.Domain.Repositories.Second' of FromRole 'Thing_Firsts_Target' in AssociationType 'Project.Domain.Repositories.Thing_Second' must exactly match with the type 'Project.Domain.Repositories.First' on which this NavigationProperty is declared on.

If I get rid of the inheritance of First and Second and directly put in Id

, Name

and ICollection<Thing> Things

, it works without issue.

There is no reason to use inheritance, other than that I have about 5 of these objects that have almost identical BaseClass

es and want to keep it DRY.

Should I just bite the bullet and repeat the code all over the place to make it easier for Entity Framework?

Am I missing something simple? Any other "received". Will I run into using inherited classes?

Does EF 6 have this support?

+3


source to share


1 answer


I would just use the attribute in the class definition of the model.

[TableName("WhateverYouWant")]
public class First{} 

      



You might want to consider "preference composition over inheritance" and treat shared values ​​as a property instead of a base class, and use an interface to handle them polymorphically. DRY is important, but more important for behavior. Two objects with the same properties should not automatically result in a base class, it is more likely that two classes with the same behavior are. Inheritance and other object-oriented methodologies are about normalizing behavior, not normalizing data and properties. After all 2 classes implementing the same interface will have duplicate property definitions; this is normal.

0


source







All Articles