c# - EF5 MVC4 Child/Parent navigation properties -
i have 3 classes can child of multitude of parent classes. there way find id/type of child's parent class without adding navigation property every possible parent type?
for example: i'm making browser based space strategy game. parent classes (fleet,asteroid,planet) inherit class called 'ownablespaceobject' 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 each of child classes (resources, structure, ship) can belong 1 of parent classes inherited base class entity framework has given them columns "asteroid_id", "planet_id", , "fleet_id" trying avoid given if add more parent classes have anywhere 3-20+ null-able 'parent_id' columns in tables (which bad practice, no?).
i want able pull parent's object can access things id child can see siblings , want try avoid putting navigation property every possibility of parents. example of in resources class need able see how many structures , ships parent has in order calculate max metal storage , metal gather rates. resources/structures/ships siblings under 1 parent fleet/asteroid/planet.
i hope makes sense , not ridiculous of request :). can point me in right direction on this?
edit clarification of class structure:
ownablespaceobject has resources/ships/structures properties(each of these own class inherits nothing, ships , structures icollections)
fleet/asteroid/planet inherit ownablespaceobject
so have:
public fleet : ownablespaceobject { //fleet specific stuff } public asteroid: ownablespaceobject { //asteroid specific stuff } public planet: ownablespaceobject { //planet specific stuff }
each of classes has icollection of stuctures , ships. entity framework makes column "asteroid_id", "planet_id", , "fleet_id" in structure/ship table each class inheriting ownablespaceobject.
in general, if resources/structures/ships inherit interface such as
public interface iresourceitem { double maxmetalstorage{get;} double metalgatherrate{get;} }
you modify parent class such
public class resources { public ilist<iresourceitem> resourceitems {get; set;} public double gettotalmetalstorage(){ return this.resourceitems.sum(x => x.maxmetalstorage); } }
Comments
Post a Comment