简体   繁体   中英

EF5 MVC4 Child/Parent navigation properties

I have 3 classes that can be the child of a multitude of parent classes. 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?

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:

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?).

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. 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)

Fleet/Asteroid/Planet inherit from 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. 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.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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