public void Validate_Multiple_Entities() { using (var context = new CodeCampContext(TestHelpers.TestDatabaseName)) { Speaker speaker = new Speaker { FirstName = "Test", Presentations = new Collection<Presentation>(new[] {new Presentation()}) }; Presentation presentation = new Presentation {Speaker = new Speaker()}; CodeCampEvent codeCamp = new CodeCampEvent { Topics = new Collection<EventSession>(new []{new EventSession{Presentation = presentation} }) }; context.Speakers.Add(speaker); context.CodeCampEvents.Add(codeCamp); TestHelpers.WriteValiationResults(context); } }
public void CRUD_With_Relationships() { // Add a new presentation through a Speaker's presentation collection. using (var context = new CodeCampContext(TestHelpers.TestDatabaseName)) { Presentation presentation = new Presentation { Title = "Another presentation about .NET" }; Speaker speaker = context.Speakers.FirstOrDefault(s => s.LastName == "Pugh"); speaker.Presentations.Add(presentation); context.SaveChanges(); var query = from p in context.Presentations where p.Speaker.LastName == "Pugh" select p; Assert.AreEqual(2, query.Count()); } // Add a speaker to a new presentation. using (var context = new CodeCampContext(TestHelpers.TestDatabaseName)) { var query = from s in context.Speakers where s.Id == 1 select s; Speaker speaker = query.FirstOrDefault(); Presentation presentation = new Presentation { Title = "More about REST", Speaker = speaker }; context.Presentations.Add(presentation); context.SaveChanges(); // Requery Speaker, this time get presentations. speaker = query.Include(s => s.Presentations).FirstOrDefault(); Assert.AreEqual(2, speaker.Presentations.Count); Console.WriteLine("New Presentation added to existing Speaker"); foreach (var presentation1 in context.Presentations.Local) { Console.WriteLine("Speaker: {0}, Presentation: {1}", presentation1.Speaker.FirstName, presentation1.Title); } Console.WriteLine(); // Change speaker of presentation var speaker2 = context.Speakers.Include(s => s.Presentations).FirstOrDefault(s => s.Id == 8); presentation.Speaker = speaker2; context.SaveChanges(); Console.WriteLine("Presenation moved to a different speaker"); foreach (var presentation1 in context.Presentations.Local.OrderBy(p=>p.Speaker.LastName)) { Console.WriteLine("Speaker: {0}, Presentation: {1}", presentation1.Speaker.FirstName, presentation1.Title); } Console.WriteLine(); // Remove Presentation by setting Speaker to null in Presentation record. presentation.Speaker = null; // This only works because Speaker is not required. If it was required an exception would be thrown on save. // Remove Presentation by removing it from the Presentation navigation property in Speaker; Presentation presentation2 = speaker2.Presentations.FirstOrDefault(); speaker2.Presentations.Remove(presentation2); context.SaveChanges(); Console.WriteLine("Presenation removed. No longer assigned to any speaker."); foreach (var presentation1 in context.Presentations.Local) { Console.WriteLine("Speaker: {0}, Presentation: {1}", presentation1.Speaker != null ? presentation1.Speaker.FirstName : "No Speaker Assigned", presentation1.Title); } } }