删除断开模式下的多个实体

时间:2015-12-09 15:10:26

标签: c# entity-framework

我正在开发一个与实体/表FloorPlan交互的应用程序(EF6 Code First方法),它有10条记录。我想删除前6条记录,因为这些记录已经过时,有新的业务需求。这是表格目前的样子:

Data in FloorPlan table

要在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中找不到该对象。

我也尝试过不改变实体的状态但无济于事。我只想在断开连接模式下这样做。你们可以针对这个问题提出一些指示吗?

2 个答案:

答案 0 :(得分:2)

如果要删除这些记录,您只需要使用现有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();

答案 1 :(得分:0)

您正在从newdbContext.FloorPlan中删除对象,请您从floorPlansOld获取它们。

对我来说,这看起来完全错了。

试试这个

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