public static void Init() { using (var ctx = new ConfContext()) { ctx.People.Add(new Person { Name = "FooBar", Age = 26, Sessions = new List <Session> { new Session { Name = "Ses1" }, new Session { Name = "Ses2" }, new Session { Name = "Ses3" }, new Session { Name = "Ses4" }, new Session { Name = "Ses5" }, new Session { Name = "Ses6" } } }); ctx.People.Add(new Person { Name = "Tugberk", Age = 26, Sessions = new List <Session> { new Session { Name = "Ses1" }, new Session { Name = "Ses2" }, new Session { Name = "Ses3" }, new Session { Name = "Ses4" }, new Session { Name = "Ses5" }, new Session { Name = "Ses6" } } }); ctx.SaveChanges(); } }
public static void GetSessionsWithoutPerson() { // if Person property on Session is not virtual, // we will get NullReferenceException here. // if Sessions property on Person is virtual and // ProxyCreationEnabled == true, we will visit the database for each // sessions's Person property. using (var ctx = new ConfContext()) { var sessions = ctx.Sessions.ToArray(); foreach (var session in sessions) { Console.WriteLine("{0} by {1}", session.Name, session.Person.Name); } } }
public static void GetPeopleWithoutSessions() { // if Sessions property on Person is not virtual, // we will get NullReferenceException here // if Sessions property on Person is virtual and // ProxyCreationEnabled == true, we will visit the database for each // person's Sessions property. using (var ctx = new ConfContext()) { var people = ctx.People.ToArray(); foreach (var person in people) { Console.WriteLine(person.Name); foreach (var session in person.Sessions) { Console.WriteLine("\t Session: {0}", session.Name); } } } }