This is a migrated thread and some comments may be shown as answers.

remove an object from context GetChanges().GetUpdates() list

7 Answers 119 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
AndreaT
Top achievements
Rank 1
AndreaT asked on 05 Nov 2010, 12:35 PM
Hi

I have a set of objects that can be loaded or newly added to the context and then (only after been loaded or added) a set of rules determines if those objects have to be persisted.

What is the best way to achieve this? Removing objects from their respective list is correct?

Ctx.GetChanges().GetUpdates<MyObject>().Remove(obj);
//....
Ctx.GetChanges().GetInserts<MyObject>().Remove(obj);
//...

Ctx.SaveChanges();

Thanks

AndreaT

7 Answers, 1 is accepted

Sort by
0
AndreaT
Top achievements
Rank 1
answered on 05 Nov 2010, 12:46 PM
I self-reply to my post:

Collection is read-only

well, apparently there is nothing I can do other than a ClearChanges() which clears *all*  the changes not only the desidered.

AndreaT
0
IT-Als
Top achievements
Rank 1
answered on 05 Nov 2010, 02:43 PM
Hi AndreaT,

For the inserts I believe you can remove the object from the context again. Since it have never been persisted., it will just be removed from the scope without issuing a delete, nor an insert statement in SQL.

For the updates, I don't know what to do.

Regards

Henrik
0
AndreaT
Top achievements
Rank 1
answered on 05 Nov 2010, 03:23 PM
Hi Henrik,

i think you are right for the insert. For the updates the doubt still remains. This is my first large scale project with OA (we have dozens of projects my myBatis and nHibernate). At the moment OA lacks of a method to check if an object already exists in the context (I have a DAL that hide insert/update behind a generic save method), right now I intercept the exception eventually thrown by Ctx.Add, second it lacks of a method to get rid of an object from the context without implying a DB delete, that is mimicking the object has never been loaded/added to the context.

AndreaT
0
IT-Als
Top achievements
Rank 1
answered on 05 Nov 2010, 03:33 PM
Hi AndreaT,

Did you check the answer Jan was giving you on this thread?

/Henrik
0
Damyan Bogoev
Telerik team
answered on 10 Nov 2010, 05:13 PM
Hi Andrea,
You could use the approach proposed by Jan and even simplify it. You do not need to obtain the underlying IObjectScope instance for your OpenAccessContext. The OpenAccessContextBase.GetContext method will return the OpenAccessContextBase instance that manages the passed persistent object. You could refactor the IsManagedByMe method in the following way:

 

public bool IsManagedByMe(object o)
{
    OpenAccessContextBase context = OpenAccessContextBase.GetContext(o);
    return ReferenceEquals(context, this);
}

Hope that helps.



Kind regards,
Damyan Bogoev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
AndreaT
Top achievements
Rank 1
answered on 17 Nov 2010, 03:39 PM
Hi Damyan,

it sounds good. Your solution comes in handy to build a generic property IsChanged wich refers to the local context.


public bool IsChanged
{
    get
    {
                 
        OpenAccessContextBase ctx = OpenAccessContextBase.GetContext(this);
                 
        return ReferenceEquals(ctx, this) &&
                (ctx.GetChanges().GetUpdates<DbListing>().Contains(this) ||
                    ctx.GetChanges().GetInserts<DbListing>().Contains(this));
    }
}
0
Damyan Bogoev
Telerik team
answered on 17 Nov 2010, 06:11 PM
Hello AndreaT,

Yes, you could use the provided approach to check if the object is in an insert or update mode.
If any other questions arise please contact us back.

Sincerely yours,
Damyan Bogoev
the Telerik team
See What's New in Telerik OpenAccess ORM in Q3 2010!
Monday, November 15, 11 am Eastern Time: Register here>>
Monday, November 15, 10 pm Eastern Time: Register here>>
Tags
Getting Started
Asked by
AndreaT
Top achievements
Rank 1
Answers by
AndreaT
Top achievements
Rank 1
IT-Als
Top achievements
Rank 1
Damyan Bogoev
Telerik team
Share this question
or