public static void DeleteFromDatabase(GDBPlatformGame game) { using (var db = new MyDbContext()) { db.Remove(game); db.SaveChanges(); } }
public static GDBPlatformGame GetGame(int id) { using (var db = new MyDbContext()) { GDBPlatformGame g = (from a in db.GDBPlatformGame where a.id == id select a).FirstOrDefault(); return(g); } }
public static int[] SaveToDatabase(List <GDBPlatformGame> games) { using (var db = new MyDbContext()) { int added = 0; int updated = 0; // get current database context var current = db.GDBPlatformGame.AsNoTracking().ToList(); List <GDBPlatformGame> toAdd = new List <GDBPlatformGame>(); List <GDBPlatformGame> toUpdate = new List <GDBPlatformGame>(); // iterate through the games list and separete out games to be added and games to be updated foreach (var g in games) { GDBPlatformGame t = (from a in current where a.id == g.id select a).SingleOrDefault(); if (t == null) { toAdd.Add(g); added++; } else { toUpdate.Add(g); updated++; } } db.GDBPlatformGame.UpdateRange(toUpdate); db.GDBPlatformGame.AddRange(toAdd); db.SaveChanges(); //MessageBox.Show(added + " added, " + updated + " updated."); return(new int[] { added, updated }); } }