Database Reference
In-Depth Information
14-6. Optimistic Concurrency with Table Per Type Inheritance
Problem
You want to use optimistic concurrency in a model that uses Table per Type inheritance.
Solution
Let's suppose you have the tables shown in Figure 14-8 , and you want to model these tables using Table per Type
inheritance and use optimistic concurrency to ensure that updates are persisted correctly. To create the model
supporting optimistic concurrency, do the following:
1.
Add a TimeStamp column to the Person table.
2.
Create a new class that inherits from DbContext in your project.
3.
Create new POCO entities for Person, Instructor, and Student, as shown in Listing 14-11.
The Person entity should be abstract because we do not want to create a person entity
directly, while both the Instructor and Student entities will inherit from the Person entity.
Add an auto-property of type DbSet<Person> to the DbContext subclass.
4.
Figure 14-8. A database diagram with our Person table and the related Instructor and Student tables
Listing 14-11. Entity Classes Reflecting Our Table per Type Inheritance Model with the TimeStamp Property Added
to the Person Class
[Table("Person", Schema = "Chapter14")]
public abstract class Person
{
[Key]
public int PersonId { get; set; }
public string Name { get; set; }
[Timestamp]
public byte[] TimeStamp { get; set; }
}
 
Search WWH ::




Custom Search