TPH和多对多关系

4

您好,我正在使用TPH继承来建立类似的可配置项。我的TPH模型如下:

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(){}}

我需要创建订单实体与子实体的多对多和一对多/多对一关系:

  • 订单拥有多个窗户类型
  • 订单拥有多个墙壁类型
  • 订单拥有多个屋顶类型
  • 订单拥有一个屋顶尺寸
  • 订单拥有一个门尺寸
  • 订单拥有一个花园尺寸
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;}
}

然而,这并没有为RoofTypes、WallTypes等生成多对多关系……我曾经以为public virtual ICollection Orders{get;set;}会被继承。我该如何建立上述继承类与订单之间的多对多和一对多关系?

1个回答

0

我试过了你的模型。它对我来说完全没有任何改变(好吧,这并不完全正确,我将“StatusDef”更改为bool,但这不会有任何影响)。然后,“咻”——我所有的关系都起作用了。你需要做的就是编译和运行项目一次。这将创建所有的表。然后去检查你的SQL服务器。

祝你好运。


你可以通过在子表中为具有1对多关系的父表添加ID来改进你的模型。 - alikuli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接