Database Reference
In-Depth Information
[Table("Student", Schema = "Chapter14")]
public class Student : Person
{
public DateTime? EnrollmentDate { get; set; }
}
[Table("Instructor", Schema = "Chapter14")]
public class Instructor : Person
{
public DateTime? HireDate { get; set; }
}
The code in Listing 14-12 demonstrates what happens in the model when an out-of-band update happens.
Listing 14-12. Testing the Model by Applying a Rogue Update
using (var context = new EF6RecipesContext())
{
var student = new Student { Name = "Joan Williams",
EnrollmentDate = DateTime.Parse("1/12/2010") };
var instructor = new Instructor { Name = "Rodger Keller",
HireDate = DateTime.Parse("7/14/1992") };
context.People.Add(student);
context.People.Add(instructor);
context.SaveChanges();
}
using (var context = new EF6RecipesContext())
{
// find the student and update the enrollment date
var student = context.People.OfType<Student>()
.First(s => s.Name == "Joan Williams");
Console.WriteLine("Updating {0}'s enrollment date", student.Name);
// out-of-band update occurs
Console.WriteLine("[Apply rogue update]");
context.Database.ExecuteSqlCommand(@"update chapter14.person set name = 'Joan Smith'
where personId =
(select personId from chapter14.person where name = 'Joan Williams')");
// change the enrollment date
student.EnrollmentDate = DateTime.Parse("5/2/2010");
try
{
context.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
Console.WriteLine("Exception: {0}", ex.Message);
}
}
Search WWH ::




Custom Search