IEFSerializable Add(IEFSerializable obj, GalacticObjectContext c, bool saveChanges = true) { IDBObject saveme = obj.GetDBObject(); //_getDBSet(saveme).Add(saveme); Type t = saveme.GetType(); //This could use a convenient workaround... if (t == typeof(DBPSystem)) { c.Systems.AddOrUpdate((DBPSystem)saveme); } else if (t == typeof(DBMoon)) { c.Moons.AddOrUpdate((DBMoon)saveme); } else if (t == typeof(DBPort)) { c.Ports.AddOrUpdate((DBPort)saveme); } else if (t == typeof(DBPlanet)) { c.Planets.AddOrUpdate((DBPlanet)saveme); } else if (t == typeof(DBColony)) { c.Colonies.AddOrUpdate((DBColony)saveme); } else if (t == typeof(DBShip) || t.IsSubclassOf(typeof(DBShip))) { c.Ships.AddOrUpdate((DBShip)saveme); } else if (t == typeof(DBPlayer) || t.IsSubclassOf(typeof(DBPlayer))) { c.Players.AddOrUpdate((DBPlayer)saveme); } else if (t == typeof(DBAccount) || t.IsSubclassOf(typeof(DBAccount))) { c.Accounts.AddOrUpdate((DBAccount)saveme); } else if (t == typeof(PlanetLayout) || t.IsSubclassOf(typeof(PlanetLayout))) { c.Layouts.AddOrUpdate((PlanetLayout)saveme); } else if (t == typeof(ShipStats) || t.IsSubclassOf(typeof(ShipStats))) { c.ShipStats.AddOrUpdate((ShipStats)saveme); } else if (t == typeof(DBTeam)) { c.Teams.AddOrUpdate((DBTeam)saveme); } else if (t == typeof(StructureModel) || t.IsSubclassOf(typeof(StructureModel))) { c.Structures.Add((StructureModel)saveme); } else { throw new Exception("Error: Entity set not available for objects of type " + t.ToString()); } if (saveChanges) { c.SaveChanges(); } return(obj); }
/// <summary> /// Deletes an object from the db /// </summary> /// <param name="obj"></param> /// <param name="saveChanges">Force db write. Set to false when sequentially inserting multiple objects. Manual context.SaveChanges required after.</param> /// <returns></returns> public IEFSerializable Delete(IEFSerializable obj, bool saveChanges = true) { var tt = obj.GetDBObject();//TODO: This is needlessly slow, need to come back and fix later var t = obj.GetType(); if (t == typeof(DBPSystem)) { _galacticObjectContext.Systems.Attach((DBPSystem)tt); _galacticObjectContext.Systems.Remove((DBPSystem)tt); } else if (t == typeof(DBMoon)) { _galacticObjectContext.Moons.Attach((DBMoon)tt); _galacticObjectContext.Moons.Remove((DBMoon)tt); } else if (t == typeof(DBPort)) { _galacticObjectContext.Ports.Attach((DBPort)tt); _galacticObjectContext.Ports.Remove((DBPort)tt); } else if (t == typeof(DBPlanet)) { _galacticObjectContext.Planets.Attach((DBPlanet)tt); _galacticObjectContext.Planets.Remove((DBPlanet)tt); } else if (t == typeof(DBColony)) { _galacticObjectContext.Colonies.Attach((DBColony)tt); _galacticObjectContext.Colonies.Remove((DBColony)tt); } else if (t == typeof(DBShip) || t.IsSubclassOf(typeof(DBShip))) { _galacticObjectContext.Ships.Attach((DBShip)tt); _galacticObjectContext.Ships.Remove((DBShip)tt); } else if (t == typeof(DBPlayer) || t.IsSubclassOf(typeof(DBPlayer))) { _galacticObjectContext.Players.Attach((DBPlayer)tt); _galacticObjectContext.Players.Remove((DBPlayer)tt); } else if (t == typeof(DBAccount) || t.IsSubclassOf(typeof(DBAccount))) { _galacticObjectContext.Accounts.Attach((DBAccount)tt); _galacticObjectContext.Accounts.Remove((DBAccount)tt); } else if (t == typeof(PlanetLayout) || t.IsSubclassOf(typeof(PlanetLayout))) { _galacticObjectContext.Layouts.Attach((PlanetLayout)tt); _galacticObjectContext.Layouts.Remove((PlanetLayout)tt); } else if (t == typeof(ShipStats) || t.IsSubclassOf(typeof(ShipStats))) { _galacticObjectContext.ShipStats.Attach((ShipStats)tt); _galacticObjectContext.ShipStats.Remove((ShipStats)tt); } else if (t == typeof(DBTeam)) { _galacticObjectContext.Teams.Attach((DBTeam)tt); _galacticObjectContext.Teams.Remove((DBTeam)tt); } else { throw new Exception("Error: Entity set not available for objects of type " + t.ToString()); } //_getDBSet(t).Remove(t); if (saveChanges) { _galacticObjectContext.SaveChanges(); } return(obj); }
/// <summary> /// Adds the object to the database and the db context to automatically track changes /// </summary> /// <param name="obj"></param> /// <param name="saveChanges">Force db write. Set to false when sequentially inserting multiple objects. Manual context.SaveChanges required after.</param> /// <returns></returns> public IEFSerializable Add(IEFSerializable obj, bool saveChanges = true) { return(Add(obj, _galacticObjectContext, saveChanges)); }