4

Hi I'm using TPH inheritance to establish similar configurable items. My TPH Model is:

public class baseConfigItem
    {
        public int ID {get; set;}
        public string ExternalText {get; set;}
        public string InternalText { get; set; }
        public bool Active { get; set; }
        public string Group { get; set; }
        public int SortOrder { get; set; }
        public statusDef status { get; set; }

        public virtual ICollection<Order> Orders{get;set;}
        public baseConfigItem()
        {

            this.Orders= new List<Order>();

        }
    }


public class DoorTypes:baseConfigItem{public DoorTypes():base(){}}
    public class WindowType : baseConfigItem { public WindowType():base(){}}
    public class WallType : baseConfigItem { public WallType():base(){}}
    public class RoofType : baseConfigItem { public RoofType():base(){}}
    public class RoofSize : baseConfigItem { public RoofSize():base(){}}
    public class DoorSize : baseConfigItem { public DoorSize():base(){}}
    public class GardenSize : baseConfigItem { public GardenSize():base(){}}

I then need to create many to many and 1, one to many/many to one relationship with the child entities with the order entities:

  • Order has many WindowTypes
  • Order has many WallTypes
  • Order has many RoofTypes
  • Order has one RoofSize
  • Order has one DoorSize
  • Order has one GardenSize
public class Order
{
    public int ID {get; set;}
    public virtual ICollection<WindowType> WindowTypes {get; set;}
    public virtual ICollection<WallType> WallTypes {get; set;}
    public virtual ICollection<RoofType> RoofTypes {get; set;}
    public RoofSize RoofSize {get; set;}
    public DoorSize DoorSize {get; set;}
    public GardenSize GardenSize {get; set;}
}

However, this doesn't generate a many to many relationship for RoofTypes, WallTypes, etc... I had assumed that the public virtual ICollection Orders{get;set;} would be inherited. How would I be able to establish the above inherited classes with a many to many and one to many relationship with order ?

svick
  • 236,525
  • 50
  • 385
  • 514
user3836415
  • 952
  • 1
  • 10
  • 25

1 Answers1

0

I tried your model. It works for me without a single change (well, thats not entirely true, I changed the "StatusDef" to bool, but that wont make any difference.) And "poof" - I have all the relationships working. All you need to do is compile and and run the project once. That will make all the tables. Then go and check your SQL server.

Good luck.

alikuli
  • 526
  • 6
  • 18
  • PS. You can improve your model by adding Id's in the child tables for the parent in the 1 to many relationship. – alikuli Sep 11 '15 at 16:16