简体   繁体   English

实体框架.Net

[英]Entity Framework .Net

I'm new to EF and trying to delete an entry from a parent Person table and 2 child tables; 我是EF的新手,正在尝试从父Person表和2个子表中删除一个条目; Name and Address...(cascade). 名称和地址...(级联)。 When I Attach to context I get the; 当我附加上下文时,我得到了; An entity object cannot be referenced by multiple instances of IEntityChangeTracker . IEntityChangeTracker的多个实例不能引用一个实体对象。 When I erase the Attach method I get the; 当我删除Attach方法时,我得到了; The object cannot be deleted because it was not found in the ObjectStateManager error. 无法删除该对象,因为在ObjectStateManager错误中找不到该对象。 Thanks for any idea because I'm out. 感谢您提出任何建议,因为我不在。

   using (var context = new PersonEntities())
        {
            ObjectContext oc = ((IObjectContextAdapter)context).ObjectContext;
            Email[] DeleteName = SelectedEntries[CurName].Names.ToArray();
            Phone[] DeletePhone = SelectedEntries[CurAddress].Addresses.ToArray();

            foreach (name name in DeleteName)
            {
         foreach(Address address in DeleteAddress)
                {
                    foreach (Person person in SelectedEntries)
                    {
                       context.Names.Attach(name);
                       oc.DeleteObject(name);
                       context.Addresses.Attach(address);

                       oc.DeleteObject(address);

                       context.Persons.Attach(person);
                       oc.DeleteObject(person);

                       oc.ObjectStateManager.ChangeObjectState(name,
                                            System.Data.EntityState.Deleted);
                       oc.ObjectStateManager.ChangeObjectState(address,
                                             System.Data.EntityState.Deleted);
                       oc.ObjectStateManager.ChangeObjectState(person,
                                                System.Data.EntityState.Deleted);
                    }
                }
            }

To me it looks like DeleteAddress is created in a different context. 在我看来,DeleteAddress是在不同的上下文中创建的。

You should get DeleteAddress in the same using block. 您应该在同一using块中获取DeleteAddress。 Otherwise you have it in another context, then you are also trying to attach it to this context to delete it, which explains the errors. 否则,您将其放置在另一个上下文中,那么您还尝试将其附加到此上下文中以将其删除,这将解释错误。

Do your get and delete from the same context. 从相同的上下文中进行获取和删除。

your entities are in the objectcontext before of delete?, if not you need attach your entities, example: 您的实体在删除之前是否在objectcontext中?如果不是,则需要附加您的实体,例如:

for the framework 4.0: 对于框架4.0:

    EntityKey ekProducto = new EntityKey("DbCursoEntity.Productos", "Id", producto.Id);

    using (dbCurso = new DbCursoEntity())
    {
        producto.EntityKey = ekProducto; //Añado la key a la entidad.

        dbCurso.Productos.Attach(producto); //Enlanzo la entidad al ObjectSet mediante Attach.
        dbCurso.Productos.DeleteObject(producto); //Elimino la entidad.

        if (dbCurso.SaveChanges() > 0)
            return true;
        else
            return false;

    }

older framework: 较旧的框架:

            EntityKey ekProducto = new EntityKey("DbCursoEntity.Productos", "Id", producto.Id);

            using (dbCurso = new DbCursoEntity())
            {
                producto.EntityKey = ekProducto; //Añado la key a la entidad.

                dbCurso.Attach(producto); //Enlanzo la entidad mediante Attach.
                dbCurso.DeleteObject(producto); //Elimino la entidad.

                if (dbCurso.SaveChanges() > 0)
                    return true;
                else
                    return false;

            }

in this examples, my entity not are in the context, for add the context I need added an entitykey with the id. 在此示例中,我的实体不在上下文中,要添加上下文,我需要添加带有id的实体键。

You have two copies of the database context. 您有数据库上下文的两个副本。 The second is the one in your code sample. 第二个是您的代码示例中的一个。 The first is the one we can't see but know exists because of the errors you get. 第一个是我们看不到但由于存在错误而知道存在的一个。 It is the context used to generate the SelectedEntities. 它是用于生成SelectedEntities的上下文。 Every time you call new PersonEntities you get a separate context which tracks the state of all its objects. 每次调用新的PersonEntities时,您都会获得一个单独的上下文,该上下文跟踪其所有对象的状态。 The first error you get says databaseContext1 owns those objects. 您得到的第一个错误是databaseContext1拥有这些对象。 You can't give them to databaseContext2. 您不能将它们提供给databaseContext2。 This is prevented for consistency. 为了一致性,这被阻止。 The second error occurs because databaseContext2 doesn't know about the objects you are trying to delete. 发生第二个错误是因为databaseContext2不知道您要删除的对象。 You need to use the same PersonEntities to delete objects that you used to fetch them from the database. 您需要使用相同的PersonEntities删除用于从数据库中获取对象的对象。

If this isn't clear, post the code you use to fetch the database objects originally. 如果不清楚,请发布用于原始获取数据库对象的代码。

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

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