I am trying to add new record on my entity. It works fine, the problem is, the related entities are adding new records as well. Is there a way to stop related or 2nd level entities to be inserting new records as well?
Here is my sample Entity Class:
public Tracking()
{
public string Details { get; set; }
//Other properties here..
[Required]
public virtual Employee { get; set; }
}
Basically I am just getting the existing Employee record then declare it on my property, then add the the Tracking record:
Employee emp = _dbContext.EmployeeRepo.GetEmployeeByID(1001);
Tracking track = new Tracking()
{
Details = "My details here",
Employee = emp
}
_dbContext.TrackingRepo.Add(track);
_dbContext.SaveChanges();
This code works fine, the problem is that, another new Employee Record is inserted on my table Employee. Which is not what I want. I just want to add a new record of Tracking with the existing employee record.
So is there a way to do this or I am missing a configuration or code with my Entity Framework?