private static void updatePerson() { // var person = new Person { Forename = "Paul", Surname = "Kane", NoOfDaysLeft = 30 }; var person2 = new Person { Forename = "Sam", Surname = "Kane", NoOfDaysLeft = 20 }; Person person; using (var context = new HCISContext()) { person = context.Persons.FirstOrDefault(); } person.Surname = "Test"; using (var context = new HCISContext()) { context.Persons.Attach(person); context.Entry(person).State = EntityState.Modified; context.Database.Log = Console.WriteLine; context.SaveChanges(); } }
private static void removePerson2() { //use a stored procedure using (var context = new HCISContext()) { var person = context.Persons.FirstOrDefault(); context.Entry(person).State = EntityState.Deleted; int key = 1; context.Database.ExecuteSqlCommand("exec deletePerson {0}", key); context.SaveChanges(); } }
private static void getPersonGraph() { using (var context = new HCISContext()) { context.Database.Log = Console.WriteLine; //var person = context.Persons.FirstOrDefault(); //Eager Load var person = context.Persons.Include(p => p.LeaveItems).FirstOrDefault(); //Explicit Loading var person1 = context.Persons.FirstOrDefault(); context.Entry(person1).Collection(l => l.LeaveItems).Load(); //LAZY LOADING - BE CAREFUL FOR PERFORMACE //Mark the property as virtual Console.WriteLine($"Found {0}, {1}" + person.Surname, person.Id); } }