【发布时间】: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