EntityFramework-奇怪的行为

时间:2018-06-20 07:23:53

标签: c# asp.net entity-framework asp.net-web-api

我有两个名为 BeverageCapacity ClubsBeverage 的表。也有称为 BeverageCapacityController ClubsBeverageController 的控制器。这是饮料容量-表中的数据的图片:

Picture to show columns

现在,我要删除ClubsBeverageID为“ 18”的所有行:

[ApiExplorerSettings(IgnoreApi = true)]
public void RemoveByClubsBeverageID(int clubsBeverageID)
{
    // Get all BeverageCapacities
    var all = GetAll();

    // Filter the BeverageCapacites which should be deleted
    var removableEntities = all.Where(bCap => bCap.ClubsBeverageID == clubsBeverageID).ToList();

    foreach (var beverageCapacity in removableEntities)
    {
        Context.BeverageCapacity.Remove(beverageCapacity);
    }

    Context.SaveChanges();
}

我知道这可以写得更高效,更易读,但为简单起见,这已经足够了。

因此,如果我直接调用此方法(通过Swagger进行此操作),则所有行均按预期方式删除,但现在有了提示:如果我通过另一个Controller(在此方法中)调用 RemoveByClubsBeverageID 例如 ClubsBeverge ),我得到以下结果:

enter image description here

似乎删除了具有 ClubsBeverageID 的行,但ID为17的行将其 ClubsBeverageID 更改为18。

这也是 ClubsBeverageController 的代码:

[ApiExplorerSettings(IgnoreApi = true)]
public void Update(DTOUpdateClubsBeverage cBev)
{
    // Get the existing ClubsBeverage
    var cb = Get(cBev.ClubsBeverageID);

    // Remove all Capacities
    var cc = new BeverageCapacityController(Context);
    cc.RemoveByClubsBeverageID(cBev.ClubsBeverageID);

    // here are the rows already changes because of SaveChanges in BeverageCapacityController

    ...
}

我无法想象这里发生了什么,但这对我来说似乎是个错误。 而且.net框架,.net Core等都是最新的。

也许有人知道为什么会这样

谢谢!

0 个答案:

没有答案