public void EditWithConflict() { using (var db = new TagLocsDbContext()) { var location = db.Locations.Single(p => p.LocationId == 1); location.Tags = db.Tags.ToList(); db.SaveChanges(); //Conflict - will throw an exception } }
public void Create() { using (var db = new TagLocsDbContext()) { db.Locations.Add(new Location { Tags = db.Tags.Where(p => p.TagId == 2).ToList() }); db.SaveChanges(); // correctly saves the new location with TagId 2 attached } }
private static void PrintLocation(string afterCreate, int i) { Console.WriteLine(afterCreate); using (var db = new TagLocsDbContext()) { var location = db.Locations.Single(a => a.LocationId == i); var tags = string.Join(",", location.Tags.Select(a => a.TagId)); Console.WriteLine("Location {0} : Tags {1}", location.LocationId, tags); } }
public void Edit(bool clear) { using (var db = new TagLocsDbContext()) { var location = db.Locations.Single(p => p.LocationId == 1); if (clear) { location.Tags.Clear(); } location.Tags = db.Tags.Where(p => p.TagId == 1).ToList(); db.SaveChanges(); // if Clear ran locations = {1}, otherwise it is {1,2} } }