Entity Framework Delete Object Problem

时光怂恿深爱的人放手 提交于 2020-01-09 07:55:09

问题


i am getting "The object cannot be deleted because it was not found in the ObjectStateManager". while Deleting object.

here is codes ;

//first i am filling listview control.
 private void Form1_Load(object sender, EventArgs e)
    {
        FirebirdEntity asa = new FirebirdEntity();

        ObjectQuery<NEW_TABLE> sorgu = asa.NEW_TABLE;

        foreach (var item in sorgu)
        {
            ListViewItem list = new ListViewItem();
            list.Text = item.AD;
            list.SubItems.Add(item.SOYAD);
            list.Tag = item;
            listView1.Items.Add(list);

        }
//than getting New_table entity from listview's tag property.
 private void button3_Click(object sender, EventArgs e)
    {

            using (FirebirdEntity arama = new FirebirdEntity())
            {

               NEW_TABLE del = (NEW_TABLE)listView1.SelectedItems[0].Tag;
               arama.DeleteObject(del);
               arama.SaveChanges();


            }}

回答1:


You need to attach the object to the ObjectContext. Try:

NEW_TABLE del = (NEW_TABLE)listView1.SelectedItems[0].Tag;
arama.Attach(del);
arama.DeleteObject(del);
arama.SaveChanges();

Attached objects are tracked by the ObjectContext. This is needed for performing deletes and updates. You can read more about attaching objects on MSDN.

Edit to clarify attach/detach:

private void Form1_Load(object sender, EventArgs e) {
    FirebirdEntity asa = new FirebirdEntity();

    ObjectQuery<NEW_TABLE> sorgu = asa.NEW_TABLE;
    foreach (var item in sorgu) {
        asa.Detach(item);
        // add to listView1
    }
}

Also, you should wrap your use of ObjectContexts in using blocks.




回答2:


In your method "Form1_Load" you create a FIRST instance of your "FirebirdEntity" context an fill the ListViewItem with entities selected from this context

In your method "button3_Click" you create a NEW, SECOND instance of your "FirebirdEntity" context. Then you try to delete an entity in this SECOND context, which was selected in the FIRST context.

Use the same instance of your context in both of your methods and everything will work fine.

(Alternatively you can select the entity you want to delete a from your SECOND context and then delete this entity instead of the origin one)




回答3:


I ran a linq query within my DomainService methods and then deleted from the result, so while the first snippet below failed with error "The object cannot be deleted because it was not found in the ObjectStateManager", the second snippet worked.

public void DeleteSharedDoc(SharedDocs shareddoc)
{
       this.ObjectContext.SharedDocs.DeleteObject(shareddoc);
}

This worked:

public void DeleteSharedDoc(SharedDocs shareddoc)
{
var query = (from w in this.ObjectContext.SharedDocs
            where w.UserShareName == shareddoc.UserShareName
            && w.UserShareUsersEmail == shareddoc.UserShareUsersEmail
            && w.DocumentId == shareddoc.DocumentId
            select w).First();
this.ObjectContext.SharedDocs.DeleteObject(query);
}



回答4:


usually for a delete in database i use this sort of linq query, always works unless you got a foreign key restraint:

int id = convert.toint32(some text field from the page);
entity data = new entity();
var del = (from record in data.records 
          where record.id == id
          select record).FirstOrDefault();
data.deleteObject(del);
data.saveChanges();

Hope this helps.




回答5:


Assume I have an object called Department, with a surrogatekey DepartmentUUID

DDL looks like this:

CREATE TABLE [dbo].[Department] 
    ( 
          DepartmentUUID [UNIQUEIDENTIFIER] NOT NULL 
        , DepartmentName varchar(24) not null
        , CreateDate smalldatetime not null 
    )
GO


ALTER TABLE [dbo].[Department] ADD CONSTRAINT PK_Department PRIMARY KEY NONCLUSTERED (DepartmentUUID) 
GO

ALTER TABLE [dbo].[Department] ADD CONSTRAINT CK_DepartmentName_Unique UNIQUE (DepartmentName) 
GO

ALTER TABLE [dbo].[Department] ADD CONSTRAINT [DF_Department_DepartmentUUID] DEFAULT ( NEWSEQUENTIALID() ) FOR DepartmentUUID
GO

ALTER TABLE [dbo].[Department] ADD CONSTRAINT [DF_Department_CreateDate] DEFAULT ( CURRENT_TIMESTAMP ) FOR CreateDate
GO

Now for the Entity Framework code. The important parts are: 1. Using the AttachTo method. 2. Creating a temporary object, and setting the primary-key value of it. (surrogate key).

public int DeleteDepartment(Guid departmentUUID)
{

    int returnValue = 0;

    Department holder = new Department();
    holder.DepartmentUUID = departmentUUID; // DepartmentUUID is the primary key of this object (entity in the db)

    using (MyContectObject context = new MyContectObject())
    {
        context.AttachTo("Departments", holder);

        context.DeleteObject(holder);

        int numOfObjectsAffected = context.SaveChanges();
        returnValue = numOfObjectsAffected;

        context.Dispose();
    }

    return returnValue;

}



回答6:


The disgustingly terrible hack I used is this:

var attachedObject = _repository.GetObjectByKey(detachedObject.EntityKey);

Works for a lot of these issues. I was hoping to find a more elegant solution to this problem by coming here. This seems to do the trick.



来源:https://stackoverflow.com/questions/1217052/entity-framework-delete-object-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!