Question

I'm using ASP.net MVC an I have simplified but made the following models

public class Employee
{
   public int Id { get; set; }
   public string EmpName { get; set; }
}

public class Customer
{
   public int Id { get; set; }
   public string CusName { get; set; }
}

public class Task
{
   public int Id { get; set; }
   public DateTime StartDate{ get; set; }
   public DateTime? EndDate{ get; set; }
}

An Employee can have many Tasks. A Task can have Many Employees (but only at any one time) A Customer can have Many Tasks

What I'm struggling with is being able to record not when the Task starts or finishes, but when the relationship between the Employee and the Task starts and ends and a new Employee starts. I know I will need a History entity but I cannot think how I would record this. By the way, if the task ends from the Customers point of view the relationship between the Employee and the task automatically ends.

Thank you as always. If anybody has any pointers for good books or video tutorials on EF that deal with this kind of relationship it would be much appreciated.

Was it helpful?

Solution

My approach to this would be to add a CustomerId and EmployeeId to Task. Then I would create a repository and add a save method. Alternatively, you could override the SaveChanges method in your context, but I wouldn't suggest this. Either way, your save method just needs to check if the entity already exists in the database, and if so, if either the CustomerId or EmployeeId have changed from what currently exists. If it has changed, you can add a new entry to a History entity or do whatever you need done. Just make sure to also save the change to the task.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top