private static void Update_5() { using (var context = new UefaDbContext()) { Console.WriteLine("Update_5 Update properties"); var teamToUpdate = new Team() { Id = 1 }; context.Attach(teamToUpdate); teamToUpdate.Name = "Shahtar (Updated_5)"; var country2 = new Country() { Id = 2 }; context.Attach(country2); teamToUpdate.Country = country2; context.SaveChanges(); } }
private static void Update_3() { using (var context = new UefaDbContext()) { Console.WriteLine("Update_3 Explicit property modified"); var teamToUpdate = new Team() { Id = 1, Country = context.Countries.Find(1), Name = "Shahtar (Updated_3)" }; context.Attach(teamToUpdate); context.Entry(teamToUpdate).Property(t => t.Name).IsModified = true; context.SaveChanges(); } }
private static void Update_4() { using (var context = new UefaDbContext()) { Console.WriteLine("Update_4 Update properties"); var teamToUpdate = new Team() { Id = 1 }; context.Attach(teamToUpdate); teamToUpdate.Name = "Shahtar (Updated_4)"; // Cannot insert explicit value for identity column in table 'Countries' when // IDENTITY_INSERT is set to OFF. var country2 = new Country() { Id = 2 }; teamToUpdate.Country = country2; context.SaveChanges(); } }