【问题标题】:delete multiple entities in disconnected mode在断开模式下删除多个实体
【发布时间】:2015-12-10 08:45:29
【问题描述】:

我正在开发一个与实体/表 FloorPlan 交互并有 10 条记录的应用程序(EF6 代码优先方法)。我想删除前 6 条记录,因为它们已因新的业务需求而过时。以下是表格当前的外观:

为了在 Code First 方法中删除它,我尝试了以下处于断开状态的代码:

using (var newdbContext = new HomItUpContext())
{
    var floorPlansOld = new List<FloorPlan>
    {
        new FloorPlan { Name = "Unitech Uniworld Gardens 2", MainImageUrl = "//cdn.homitup.com/resources/featured-ideas/floor-plans/unitech_uniworld_gardens_2/profile.jpg", IsActive = true, FloorPlanIdeas = new List<FloorPlanIdea>() },
        new FloorPlan { Name = "Vatika Seven Lamps", MainImageUrl = "//cdn.homitup.com/resources/featured-ideas/floor-plans/vatika_seven_lamps/profile.jpg", IsActive = true, FloorPlanIdeas = new List<FloorPlanIdea>() },
        new FloorPlan { Name = "Bestech Park View Spa", MainImageUrl = "//cdn.homitup.com/resources/featured-ideas/floor-plans/bestech_park_view_spa/profile.jpg", IsActive = true, FloorPlanIdeas = new List<FloorPlanIdea>() },
        new FloorPlan { Name = "Imperia Esfera", MainImageUrl = "//cdn.homitup.com/resources/featured-ideas/floor-plans/imperia_esfera/profile.jpg", IsActive = true, FloorPlanIdeas = new List<FloorPlanIdea>() },
        new FloorPlan { Name = "Raheja Vedas", MainImageUrl = "//cdn.homitup.com/resources/featured-ideas/floor-plans/raheja_vedas/profile.jpg", IsActive = true, FloorPlanIdeas = new List<FloorPlanIdea>() },
        new FloorPlan { Name = "Tulip Violet Grandeur", MainImageUrl = "//cdn.homitup.com/resources/featured-ideas/floor-plans/tulip_violet_grandeur/profile.jpg", IsActive = true, FloorPlanIdeas = new List<FloorPlanIdea>() }
    };

    floorPlansOld.ForEach(a => newdbContext.FloorPlan.Remove(a));
    floorPlansOld.ForEach(a => newdbContext.Entry(a).State = System.Data.Entity.EntityState.Deleted);
    newdbContext.SaveChanges();
};

当我通过包管理器控制台运行update-database 命令时,出现以下错误:

无法删除该对象,因为它在 ObjectStateManager 中找不到。

我也尝试过不更改实体的状态,但无济于事。我只想在断开连接模式下进行。各位大神能指点一下这个问题吗?

【问题讨论】:

    标签: c# entity-framework


    【解决方案1】:

    如果您想删除这些记录,您唯一需要做的就是使用它们现有的 Id 创建实体实例。

    using (var newdbContext = new HomItUpContext())
    {
        var floorPlansOld = new List<FloorPlan>
        {   //Put here the record's Ids you want to delete
            new FloorPlan { Id=1 },
            new FloorPlan { Id=2 },
            new FloorPlan { Id=3 },
            new FloorPlan { Id=4 },
            new FloorPlan { Id=5 },
            new FloorPlan { Id=6 }
        };
    
        newdbContext.RemoveRange(floorPlansOld);// You can use RemoveRange method instead a foreach to call Remove method.
        newdbContext.SaveChanges();
    };
    

    更新

    好吧,在这种情况下,我建议您先查询所有要删除的实体的名称,然后您可以使用RemoveRange 方法删除它们:

    var names=new List<string>(){ "Unitech Uniworld Gardens 2", "Vatika Seven Lamps",...};
    var entitiesToDelete=newdbContext.FloorPlan.Where(fp=>names.Contains(fp.Name));
    newdbContext.RemoveRange(entitiesToDelete);
    newdbContext.SaveChanges();
    

    【讨论】:

    • 开发数据库和生产数据库的id不同。这就是为什么我倾向于使用唯一的 Name 属性进行断开模式更新的原因,尽管我完全同意您关于 RemoveRange 的建议。
    【解决方案2】:

    您正在从 newdbContext.FloorPlan 中删除对象,请从 floorPlansOld 中购买。

    在我看来完全不对。

    试试这个

    var a = newdbContext.FloorPlan.First();
    newdbContext.FloorPlan.Remove(a);
    

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      • 2022-01-09
      • 2013-01-31
      相关资源
      最近更新 更多