简体   繁体   English

以断开连接模式删除多个实体

[英]delete multiple entities in disconnected mode

I am working on an application (EF6 Code First approach) that is interacting with a entity/table FloorPlan and has 10 records. 我正在开发一个与实体/表FloorPlan交互并具有10条记录的应用程序(EF6代码优先方法)。 I want to delete first 6 records as those are obsolete with new business requirements. 我要删除前6条记录,因为这些记录已因新的业务需求而过时。 Here's how the table currently looks: 该表当前的外观如下:

FloorPlan表中的数据

To delete this in the Code First approach, I tried following code in disconnected state: 为了使用“代码优先”方法删除此代码,我尝试了以下处于断开状态的代码:

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();
};

When I run update-database command via package manager console, I get following error: 通过包管理器控制台运行update-database命令时,出现以下错误:

The object cannot be deleted because it was not found in the ObjectStateManager. 无法删除该对象,因为在ObjectStateManager中找不到该对象。

I have also tried without changing the state of the entities but to no avail. 我也尝试过不更改实体状态但无济于事。 I only want to do it in disconnected mode. 我只想在断开连接模式下进行操作。 Can you guys throws some pointers around this problem? 你们可以针对这个问题提出一些建议吗?

If you want to delete those records the only you need to do is create your entity instances with their existing Ids. 如果要删除这些记录,只需要做的就是使用其现有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();
};

Update 更新

Well, in that case I suggest you make a query first seeking all the entities you want to delete by their names, and after that you can delete them using the RemoveRange method: 好吧,在这种情况下,我建议您先进行查询,然后按名称搜索要删除的所有实体,然后可以使用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();

You are removing object from newdbContext.FloorPlan, buy you take them from floorPlansOld. 您正在从newdbContext.FloorPlan中删除对象,请从floorPlansOld中获取它们。

It looks completely wrong to me. 在我看来,这完全是错的。

Try this 尝试这个

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

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

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