简体   繁体   English

在实体框架CF中删除相关项目

[英]Remove related item in entity framework CF

I'm trying to remove an item from database, 我正在尝试从数据库中删除一个项目,

This item is related to other items. 此项目与其他项目相关。 when i try to remove it like this. 当我尝试像这样删除它时。

using (var ctx = new UmeaKommunFacilityContext())
                        {
                            try
                            {
                                ctx.Facility.Attach(oldFacilitiesToDelete[i]);
                                ctx.Facility.Remove(oldFacilitiesToDelete[i]);
                                ctx.SaveChanges();
                            }
                            catch (Exception e)
                            { 

                            }
                        }

then i'm getting 然后我得到

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

i added this code to detach the item 我添加了此代码以分离项目

ctx.Entry(oldFacilitiesToDelete[i]).State = EntityState.Detached;
                        ctx.Facility.Attach(oldFacilitiesToDelete[i]);
                        ctx.Facility.Remove(oldFacilitiesToDelete[i]);
                        ctx.SaveChanges();

then i got the same exception. 然后我有同样的例外。

then what to do to be able to remove the item from db. 然后该怎么做才能从数据库中删除该项目。 this item type (Facility) is related to other types, and i have removed all aother related by this code and it worked 此项目类型(设施)与其他类型相关,并且我已删除了与此代码相关的所有其他内容,并且可以正常工作

  using (var db2 = new UmeaKommunFacilityContext())
                {
                    try
                    {

                        Facility f = oldFacilitiesToDelete[i];
                        List<Image> images = f.Images.ToList<Image>();
                        for (int j = 0; j < images.Count; j++)
                        {
                            db.Images.Attach(images[i]);
                            db.Images.Remove(images[i]);
                            db.Entry(images[i]).State = EntityState.Detached;

                        }


                        List<Material> materials = f.Materials;
                        for (int j = 0; j < materials.Count; j++)
                        {
                            db.Materials.Attach(materials[i]);
                            db.Materials.Remove(materials[i]);
                            db.Entry(materials[i]).State = EntityState.Detached;

                        }


                        List<Polygon> polygons = f.Polygons;
                        for (int j = 0; j < polygons.Count; j++)
                        {
                            db.Polygons.Attach(polygons[i]);
                            db.Polygons.Remove(polygons[i]);
                            db.Entry(polygons[i]).State = EntityState.Detached;
                        }


                        db.SaveChanges();

                    }
                    catch (EntityException ex)
                    {
                    }
                    catch (Exception e)
                    {  }

                }

Why are you doing Attach() and Remove() on the same item? 为什么要在同一项目上执行Attach()Remove()

To remove an item from database you need to use the Delete action. 要从数据库中删除项目,您需要使用Delete操作。 For example: 例如:

ctx.Entry(oldFacilitiesToDelete[i]).State = EntityState.Deleted;
ctx.SaveChanges();

When you "Detach" an item you're simply removing it from the context, not from the database. 当“分离”一个项目时,您只是将其从上下文而不是从数据库中删除。 That why you can always reverse with "Attach". 这就是为什么您总是可以通过“附加”来撤消。

If the point here is to remove the related entity for an items, it's easier to implement .OnCascadeDelete(true) so you can remove only the main entity; 如果此处的重点是删除项目的相关实体,则实现.OnCascadeDelete(true)会更容易,因此您只能删除主要实体; EF will remove the other related items for you, like this: EF会为您删除其他相关项目,例如:

        HasRequired(t => t.MainEntity)
          .WithMany(t => t.SubEntity)
          .HasForeignKey(d => d.MainEntityId)
          .WillCascadeOnDelete(true);

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

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