Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有多个父类型的实体框架子级_C#_Asp.net_Entity Framework_Asp.net Mvc 4_Entity Framework 4 - Fatal编程技术网

C# 具有多个父类型的实体框架子级

C# 具有多个父类型的实体框架子级,c#,asp.net,entity-framework,asp.net-mvc-4,entity-framework-4,C#,Asp.net,Entity Framework,Asp.net Mvc 4,Entity Framework 4,我有3个类继承自OwnableSpaceObject,该类通过ICollections向其子级提供2个导航属性 从OwnableSpaceObject中,我希望能够有一个像AddChild这样的类,它将把一个子类添加到ICollection中,并将其保存到数据库中 以下是OwnableSpaceObject基类: public abstract class OwnableSpaceObject : SpaceObject { public int? UserId { get; set;

我有3个类继承自OwnableSpaceObject,该类通过ICollections向其子级提供2个导航属性

OwnableSpaceObject中,我希望能够有一个像AddChild这样的类,它将把一个子类添加到ICollection中,并将其保存到数据库中

以下是OwnableSpaceObject基类:

public abstract class OwnableSpaceObject : SpaceObject
{
    public int? UserId { get; set; }
    public int? ResourcesId { get; set; }

    [ForeignKey("UserId")]
    public virtual UserProfile User { get; set; }

    [ForeignKey("ResourcesId")]
    public virtual Resources Resources { get; set; }

    public virtual ICollection<Structure> Structures { get; set; }
    public virtual ICollection<Ship> Ships { get; set; }
}
类似地,用于添加船舶的重载CheckOrAddChild:

public virtual Ship CheckOrAddChild(ShipType _shipType)
        {
            using (ChronosContext db = new ChronosContext())
            {
                var ship = Ships != null ? Ships.FirstOrDefault(x => x.ShipTypeId == _shipType.Id) : null;
                if (ship == null)
                {
                    Ship newShip = new Ship(_shipType.Id);
                    Ships.Add(newShip); //this should add the Ship to the database and link it to the parent OwnableSpaceObject right? It errors out right here saying that Ships is null
                    db.SaveChanges();

                    ship = newShip;
                }
                return ship;
            }
        }
以下是船舶和结构类的基本情况:

public class Ship
    {
        public int Id { get; set; }
        public int CurrentAmount { get; set; }
        public int BuildingAmount { get; set; }

        public int ShipTypeId { get; set; }

        [ForeignKey("ShipTypeId")]
        public virtual ShipType ShipType { get; set; }
}
Ship/Structure类没有OwnableSpaceObject的导航属性,因为它为我的所有舰队/小行星/行星装箱一个巨大的表,因为它们都继承自OwnableSpaceObject。我想让舰队/小行星/行星在表格中分开,但仍然能够将船只和结构连接到它们上。目前,EF正在船舶/结构表中制作3列,分别命名为“小行星Id”、“行星Id”和“舰队Id”。我是否应该为每一个属性创建一个导航属性,然后自己手动将它们链接起来?为了避免重复代码,我试图避免这种情况

有人对此有什么想法吗?在过去的两天里我一直在研究,我快要精神崩溃了

public class Ship
    {
        public int Id { get; set; }
        public int CurrentAmount { get; set; }
        public int BuildingAmount { get; set; }

        public int ShipTypeId { get; set; }

        [ForeignKey("ShipTypeId")]
        public virtual ShipType ShipType { get; set; }
}