Database Reference
In-Depth Information
Figure 12-1. A model for job applicant
To ensure that the applicant's resume file is deleted when the applicant is deleted, we override the
SavingChanges() method in the DbContext. In our overridden method, we need to scan the DbContext for changes
that include deleting instances of the Applicant entity. Next we need to tell Entity Framework to save the changes by
calling the real SaveChanges() method. Finally, for each of the deleted Applicants, we need to delete the associated
resume file. The code in Listing 12-1 demonstrates this approach.
Listing 12-1. Overriding SaveChanges() to Delete the Resume File When the Applicant Is Deleted
class Program
{
static void Main(string[] args)
{
RunExample();
}
static void RunExample()
{
using (var context = new EFRecipesEntities())
{
var path1 = "AlexJones.txt";
File.AppendAllText(path1, "Alex Jones\nResume\n...");
var path2 = "JanisRogers.txt";
File.AppendAllText(path2, "Janis Rodgers\nResume\n...");
var app1 = new Applicant
{
Name = "Alex Jones",
ResumePath = path1
};
var app2 = new Applicant
{
Name = "Janis Rogers",
ResumePath = path2
};
context.Applicants.Add(app1);
context.Applicants.Add(app2);
context.SaveChanges();
 
Search WWH ::




Custom Search