简体   繁体   English

EF5 MVC4子级/父级导航属性

[英]EF5 MVC4 Child/Parent navigation properties

I have 3 classes that can be the child of a multitude of parent classes. 我有3个班级可以成为众多父班级的孩子。 Is there a way to find the ID/Type of the child's parent class without adding a navigation property for every possible parent type? 有没有一种方法可以找到孩子的父类的ID /类型,而不必为每种可能的父类型添加导航属性?

For example: I'm making a browser based space strategy game. 例如:我正在制作一个基于浏览器的太空策略游戏。 The parent classes (Fleet,Asteroid,Planet) inherit from a class called 'OwnableSpaceObject' as defined here: 父类(Fleet,Asteroid,Planet)继承自称为“ 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; }
}

Given that each of the child classes (Resources, Structure, Ship) can belong to any one of the parent classes that inherited from that base class Entity Framework has given them columns like "Asteroid_Id", "Planet_Id", and "Fleet_Id" which I was trying to avoid given that if I add more parent classes I will have anywhere from 3-20+ null-able 'Parent_Id' columns in the tables (which is bad practice, no?). 鉴于每个子类(资源,结构,船)都可以属于从该基础类实体框架继承的任何父类,因此我给了它们“ Asteroid_Id”,“ Planet_Id”和“ Fleet_Id”列试图避免考虑到如果我添加更多的父类,那么表中将有3-20个以上可空的'Parent_Id'列(这是不好的做法,不是吗?)。

I want to be able to pull the Parent's object so that I can access things like Id from the child so that I can see what their siblings are and I want to try to avoid putting a navigation property for EVERY possibility of their parents. 我希望能够拉动“父母”的对象,以便可以从孩子那里访问诸如“ Id”之类的东西,这样我就可以看到他们的兄弟姐妹是什么,并且我想尽量避免为父母的所有可能性设置导航属性。 An example of this is that in the Resources class I need to be able to see how many Structures and Ships the parent has in order to calculate the Max Metal storage and Metal Gather rates. 这样的一个示例是,在资源类中,我需要能够看到父级拥有多少个结构装运 ,以便计算最大金属存储量和金属收集速率。 So the Resources/Structures/Ships are all siblings under one parent which could be a Fleet/Asteroid/Planet . 因此, 资源/结构/船舶都是同一个父母下的兄弟姐妹,可能是舰队/小行星/行星

I hope this all makes sense and that its not too ridiculous of a request :). 我希望这一切都有意义,并且不要太荒谬地提出请求:)。 Can anyone point me in the right direction on this? 有人可以为此指出正确的方向吗?

EDIT for clarification of Class structure: 编辑以澄清类结构:

OwnableSpaceObject has Resources/Ships/Structures as properties(each of these are their own class which inherits from nothing, Ships and Structures are ICollections) OwnableSpaceObject具有资源/船舶/结构作为属性(每个属性都是它们自己的类,它们不继承任何东西,船舶和结构是ICollections)

Fleet/Asteroid/Planet inherit from OwnableSpaceObject 舰队/小行星/行星从OwnableSpaceObject继承

So basically I have: 所以基本上我有:

public Fleet : OwnableSpaceObject { //fleet specific stuff }
public Asteroid: OwnableSpaceObject { //asteroid specific stuff }
public Planet: OwnableSpaceObject { //planet specific stuff }

Each of those classes has an ICollection of Stuctures and Ships. 每个此类都有一个Itu的Stuctures and Ships。 Entity framework makes a column like "Asteroid_Id", "Planet_Id", and "Fleet_Id" in the Structure/Ship table for each class that is inheriting from OwnableSpaceObject. 实体框架为从OwnableSpaceObject继承的每个类在Structure / Ship表中创建一列,例如“ Asteroid_Id”,“ Planet_Id”和“ Fleet_Id”。

In general, if Resources/Structures/Ships inherit from an interface such as 通常,如果资源/结构/船舶从诸如

public interface IResourceItem
{
   double MaxMetalStorage{get;}
   double MetalGatherRate{get;}
}

You could modify the parent class as such 您可以这样修改父类

public class Resources
{
    public IList<IResourceItem> ResourceItems {get; set;}

    public double GetTotalMetalStorage(){
        return this.ResourceItems.Sum(x => x.MaxMetalStorage);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM