public void Verify_that_deletes_precede_inserts() { using (var context = new GearsOfWarContext()) { var squad1 = new Squad { Id = 3, Name = "Alpha", }; context.Squads.Add(squad1); context.SaveChanges(); var squad2 = new Squad { Id = 3, Name = "Bravo", }; context.Squads.Add(squad2); context.Squads.Remove(squad1); context.SaveChanges(); Assert.Equal( "Bravo", context.Squads.Where(o => o.Id == 3).Select(s => s.Name).Single()); } }
public void Verify_that_deletes_precede_inserts() { ExtendedSqlAzureExecutionStrategy.ExecuteNew( () => { using (new TransactionScope()) { using (var context = new GearsOfWarContext()) { var squad1 = new Squad { Id = 3, Name = "Alpha", }; context.Squads.Add(squad1); context.SaveChanges(); var squad2 = new Squad { Id = 3, Name = "Bravo", }; context.Squads.Add(squad2); context.Squads.Remove(squad1); context.SaveChanges(); Assert.Equal( "Bravo", context.Squads.Where(o => o.Id == 3).Select(s => s.Name).Single()); } } }); }
public void Modifying_identity_non_key_throws() { using (var context = new GearsOfWarContext()) { var squad = new Squad { Id = 10, Name = "Lima", }; context.Squads.Add(squad); context.SaveChanges(); var squadInternalNumber = squad.InternalNumber; squad.InternalNumber = squadInternalNumber + 1; Assert.Throws<DbUpdateException>(() => context.SaveChanges()) .InnerException.InnerException.ValidateMessage( typeof(DbContext).Assembly(), "Update_ModifyingIdentityColumn", null, "Identity", "InternalNumber", "CodeFirstDatabaseSchema.Squad"); } }
public void Modifying_non_generated_key_throws() { using (var context = new GearsOfWarContext()) { var squad = new Squad { Id = 10, Name = "Lima", }; context.Squads.Add(squad); context.SaveChanges(); squad.Id = 20; Assert.Throws<InvalidOperationException>(() => context.SaveChanges()) .ValidateMessage(typeof(DbContext).Assembly(), "ObjectStateEntry_CannotModifyKeyProperty", null, "Id"); } }
public void Cascade_delete_works_properly_on_one_to_many_relationship() { using (var context = new GearsOfWarContext()) { var gearsBefore = context.Gears.Count(); var gear1 = new Gear { FullName = "Gear1", Nickname = "Gear1", Tag = new CogTag { Id = Guid.NewGuid(), Note = "Tag1", }, }; var gear2 = new Gear { FullName = "Gear2", Nickname = "Gear2", Tag = new CogTag { Id = Guid.NewGuid(), Note = "Tag2", }, }; var squad = new Squad { Name = "Charlie", Members = new List<Gear> { gear1, gear2 }, }; context.Squads.Add(squad); context.SaveChanges(); var gearsAfterAdd = context.Gears.Count(); context.Squads.Remove(squad); context.SaveChanges(); var gearsAfterRemove = context.Gears.Count(); Assert.Equal(gearsBefore, gearsAfterRemove); Assert.Equal(gearsBefore + 2, gearsAfterAdd); } }
public void Inserting_entity_that_references_itself_in_one_to_many_relationship_works() { using (var context = new GearsOfWarContext()) { var squad = new Squad { Name = "One Man Squad", }; var tag = new CogTag { Id = Guid.NewGuid(), Note = "Tag", }; var oneManArmy = new Gear { FullName = "One Man Army", Nickname = "OMA", Rank = MilitaryRank.Private, Squad = squad, Tag = tag, }; oneManArmy.Reports = new List<Gear> { oneManArmy }; context.Gears.Add(oneManArmy); context.SaveChanges(); var oma = context.Gears.Where(g => g.Nickname == "OMA").Include(g => g.Reports).Single(); Assert.Same(oma, oma.Reports.Single()); } }
public void Seed(GearsOfWarContext context) { var lancer = new StandardWeapon { Name = "Lancer", Specs = new WeaponSpecification { AmmoPerClip = 60, ClipsCount = 8, } }; var gnasher = new StandardWeapon { Name = "Gnasher", Specs = new WeaponSpecification { AmmoPerClip = 8, ClipsCount = 6, }, SynergyWith = lancer, }; var hammerburst = new StandardWeapon { Name = "Hammerburst", Specs = new WeaponSpecification { AmmoPerClip = 20, ClipsCount = 7, } }; var markza = new StandardWeapon { Name = "Markza", Specs = new WeaponSpecification { AmmoPerClip = 10, ClipsCount = 12, }, SynergyWith = gnasher, }; var mulcher = new HeavyWeapon { Name = "Mulcher", Overheats = true, }; context.Weapons.AddRange(new List<Weapon> { lancer, gnasher, hammerburst, markza, mulcher, }); var deltaSquad = new Squad { Id = 1, Name = "Delta", }; var kiloSquad = new Squad { Id = 2, Name = "Kilo", }; context.Squads.AddRange(new[] { deltaSquad, kiloSquad }); var jacinto = new City { Location = DbGeography.FromText("POINT(1 1)", DbGeography.DefaultCoordinateSystemId), Name = "Jacinto", }; var ephyra = new City { Location = DbGeography.FromText("POINT(2 2)", DbGeography.DefaultCoordinateSystemId), Name = "Ephyra", }; var hanover = new City { Location = DbGeography.FromText("POINT(3 3)", DbGeography.DefaultCoordinateSystemId), Name = "Hanover", }; context.Cities.AddRange(new[] { jacinto, ephyra, hanover }); var marcusTag = new CogTag { Id = Guid.NewGuid(), Note = "Marcus's Tag", }; var domsTag = new CogTag { Id = Guid.NewGuid(), Note = "Dom's Tag", }; var colesTag = new CogTag { Id = Guid.NewGuid(), Note = "Cole's Tag", }; var bairdsTag = new CogTag { Id = Guid.NewGuid(), Note = "Bairds's Tag", }; var paduksTag = new CogTag { Id = Guid.NewGuid(), Note = "Paduk's Tag", }; var kiaTag = new CogTag { Id = Guid.NewGuid(), Note = "K.I.A.", }; context.Tags.AddRange( new[] { marcusTag, domsTag, colesTag, bairdsTag, paduksTag, kiaTag }); var marcus = new Gear { Nickname = "Marcus", FullName = "Marcus Fenix", Squad = deltaSquad, Rank = MilitaryRank.Sergeant, Tag = marcusTag, CityOfBirth = jacinto, Weapons = new List<Weapon> { lancer, gnasher }, }; var dom = new Gear { Nickname = "Dom", FullName = "Dominic Santiago", Squad = deltaSquad, Rank = MilitaryRank.Corporal, Tag = domsTag, CityOfBirth = ephyra, Weapons = new List<Weapon> { hammerburst, gnasher } }; var cole = new Gear { Nickname = "Cole Train", FullName = "Augustus Cole", Squad = deltaSquad, Rank = MilitaryRank.Private, Tag = colesTag, CityOfBirth = hanover, Weapons = new List<Weapon> { gnasher, mulcher } }; var baird = new Gear { Nickname = "Baird", FullName = "Damon Baird", Squad = deltaSquad, Rank = MilitaryRank.Corporal, Tag = bairdsTag, Weapons = new List<Weapon> { lancer, gnasher } }; var paduk = new Gear { Nickname = "Paduk", FullName = "Garron Paduk", Squad = kiloSquad, Rank = MilitaryRank.Private, Tag = paduksTag, Weapons = new List<Weapon> { markza }, }; marcus.Reports = new List<Gear> { dom, cole, baird }; baird.Reports = new List<Gear> { paduk }; context.Gears.AddRange(new[] { marcus, dom, cole, baird, paduk }); context.SaveChanges(); }