Why does Entity Framework (Core) delete old records in Add () operations?

I have an organization that needs a history tracked on it to pay employees. EmployeeRate object:

public class EmployeeRate : BaseEntity { [Key] public int EmployeeRateId { get; set; } public int EmployeeId { get; set; } public double Rate { get; set; } } public class BaseEntity { public bool ActiveFlag { get; set; } public DateTime CreateStamp { get; set; } public DateTime UpdateStamp { get; set; } public string LastModifiedBy { get; set; } } 

So, in order to keep track of the history, when I “update” the level of employees, I retrieve all the old records with their identifier and set the ActiveFlag value to false, and then add a new object with a new payment rate, and the ActiveFlag value is true. Here is my function for this operation:

 private EmployeeRate UpdateRate(EmployeeRate model, string currentUser) { var existingRates = _context.EmployeeRates.Where(r => r.EmployeeId == model.EmployeeId).ToList(); foreach(var rate in existingRates) { rate.ActiveFlag = false; rate.UpdateStamp = DateTime.UtcNow; rate.LastModifiedBy = currentUser; } _context.SaveChanges(); var newRate = new EmployeeRate() { EmployeeId = model.EmployeeId, Rate = model.Rate, ActiveFlag = true, CreateStamp = DateTime.UtcNow, UpdateStamp = DateTime.UtcNow, LastModifiedBy = currentUser }; newRate = _context.EmployeeRates.Add(newRate).Entity; _context.SaveChanges(); return newRate; } 

When I do the Add operation on my DbContext, the SQL generated by EntityFramework for some reason deletes all the old records for this employee.
Generated SQL:

REMOVE FROM employeerates WHERE EmployeeRateId = @ p0;

ActiveFlag employeerates ( ActiveFlag , CreateStamp , EmployeeId , LastModifiedBy , Rate , UpdateStamp ) VALUES (@ p1, @ p2, @ p3, @ p4, @ p5, @ p6); SELECT EmployeeRateId FROM employeerates WHERE ROW_COUNT () = 1 AND EmployeeRateId = LAST_INSERT_ID ();

Why is this happening? Is there a way to keep EF from deleting these old entries?

EDIT

I use asp.net core core and entity core.

+5
source share
1 answer

Get ready to call me stupid ...

Due to the way I want to track the history on the EmployeeRate object, I was thinking incorrectly about my relationship between Employee and EmployeeRate. Here was my original Employee object (which I had to publish initially):

  public class Employee : BaseEntity { public int EmployeeId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public string PhoneNumber { get; set; } public int HomeLocationId { get; set; } //Navigation Properties public virtual Location HomeLocation { get; set; } public virtual EmployeeRate EmployeeRate { get; set; } } 

Some of you may immediately see the problem. The EmployeeRate link should actually be like this:

 public virtual List<EmployeeRate> EmployeeRates { get; set; } 

because it’s actually a one to many relationship. Due to the fact that I was only thinking about having one bet per employee, I had the only link before (making it one on one from the point of view of EF). So when I updated the link, EF thought it was one to one, so he deleted all the other entries with the same link. It’s actually “One for many” with only ONE Active bid at a time. Thus, the Navigation EmployeeRate property must be called as List or Collection for EF for proper operation, and not for deleting old records. Stupid me;)

+1
source

Source: https://habr.com/ru/post/1261020/


All Articles