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