0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I couldn't think about the relationship. Difficult some times. Once a user is removed, a new instance of them is created in the historical daatbase. easy!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.