简体   繁体   English

为什么实体框架(核心)在Add()操作上删除旧记录?

[英]Why is Entity Framework (Core) deleting old records on Add() operation?

I have an entity that needs history tracked on it for employee pay rates. 我有一个需要在其上跟踪员工工资历史记录的实体。 The employeeRate object is: employeeRate对象是:

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 to track history, when I "update" an employees rate, I retrieve all old records with their Id and set the ActiveFlag to false, then add a new entity with the new pay rate and the ActiveFlag set to true. 因此,为了跟踪历史记录,当我“更新”员工费率时,我将检索所有具有其ID的旧记录,并将ActiveFlag设置为false,然后添加具有新工资率并将ActiveFlag设置为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 an Add operation on my DbContext object, the SQL being generated by EntityFramework is deleting all old records for that employee for some reason. 当我在DbContext对象上执行Add操作时,由于某种原因,EntityFramework生成的SQL正在删除该员工的所有旧记录。
Generated SQL: 生成的SQL:

DELETE FROM employeerates WHERE EmployeeRateId = @p0; employeerates删除WHERE EmployeeRateId = @ p0;

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

Why would this be happening? 为什么会这样呢? Is there a way to keep EF from deleting these old records? 有没有办法阻止EF删除这些旧记录?

EDIT 编辑

I am using asp.net core and entity framework core. 我正在使用asp.net核心和实体框架核心。

Prepare to call me stupid... 准备称我为愚蠢...

Because of how I want to track history on the EmployeeRate object, I was thinking of my relationships between Employee and EmployeeRate incorrectly. 由于我想如何跟踪EmployeeRate对象的历史记录,因此我在错误地考虑了Employee和EmployeeRate之间的关系。 Here was my original Employee object (which I should've posted originally): 这是我原来的Employee对象(我应该原先发布):

    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 see the problem right away here. 你们中有些人可能会立即在这里看到问题。 The EmployeeRate reference actually has to be this: EmployeeRate参考实际上必须是这样的:

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

because it is actually a one to many relationship. 因为它实际上是一对多的关系。 Due to how I was thinking about only having to have one rate per employee, I only had the singular reference before (making it one to one as far as EF is concerned). 由于我当时的想法是每位员工只需要有一个费率,因此我以前只有一个参考(就EF而言是一对一的)。 So when I updated the reference, EF thought it was one to one so it deleted all other records with the same reference. 因此,当我更新引用时,EF认为它是一对一的,因此它删除了具有相同引用的所有其他记录。 It's actually "One to Many" with only ONE rate "Active" at a time. 它实际上是“一对多”,一次仅具有一个速率“活动”。 So the EmployeeRate navigation property needs to be referenced as a List or Collection for EF to work properly and not delete old records. 因此,EmployeeRate导航属性需要作为EF的列表或集合来引用,以使其正常工作并且不删除旧记录。 Stupid me ;) 愚蠢的我 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM