Reputation: 101
I'm having a trouble configuring May-to-many with TPC inheritance
public class TestB
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<ParentClass> ParentClasss { get; set; }
}
public abstract class ParentClass
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<TestB> TestBs { get; set; }
}
[Table("Child_A")]
public class Child_A: ParentClass
{
public string childAName { get; set; }
}
[Table("Child_B")]
public class Child_B: ParentClass
{
public string childbName { get; set; }
}
the many to many relation is on the abstract class, the generated tables are
TestB
ParentClass
ParentClassTestB : the many to many relation
Child_A : have FK for the ParentClass
Child_B : have FK for the ParentClass
what I need is to have the many to many directly with Child_A and Child_B.
so the generated tables will be something like
TestB
Child_A
Child_ATestB : the many to many relation table between Child_A and TestB
Child_B
Child_BTestB : the many to many relation table between Child_B and TestB
regards
Upvotes: 0
Views: 448
Reputation: 88852
what i need is to have the many to many directly with Child_A and Child_B.
Then don't map ParentClass as an Entity. You can still have it as the parent class in your code, but as far as the database is concerned Clild_A and Child_B are unrelated.
This is especially important in TPH, which has serious performance implications. This way there's simply no database overhead for your inheritance hierarchy. And the only real downside is that you don't have a built-in search over all ParentClass entities.
The only change is that if you don't want ParentClass involved in the M2M in the database, you can't have a navigation property from TestB to ParentClass. So
public class TestB
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Child_A> ParentClasssA { get; set; }
public ICollection<Child_B> ParentClasssB { get; set; }
}
public abstract class ParentClass
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<TestB> TestBs { get; set; }
}
[Table("Child_A")]
public class Child_A : ParentClass
{
public string childAName { get; set; }
}
[Table("Child_B")]
public class Child_B : ParentClass
{
public string childbName { get; set; }
}
Upvotes: 1