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

8 posts, 0 answers
  1. AndreaT
    AndreaT avatar
    27 posts
    Member since:
    Sep 2007

    Posted 05 Nov 2010 Link to this post

    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
  2. AndreaT
    AndreaT avatar
    27 posts
    Member since:
    Sep 2007

    Posted 05 Nov 2010 Link to this post

    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
  3. DevCraft banner
  4. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 05 Nov 2010 Link to this post

    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
  5. AndreaT
    AndreaT avatar
    27 posts
    Member since:
    Sep 2007

    Posted 05 Nov 2010 Link to this post

    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
  6. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 05 Nov 2010 Link to this post

    Hi AndreaT,

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

    /Henrik
  7. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 10 Nov 2010 Link to this post

    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
  8. AndreaT
    AndreaT avatar
    27 posts
    Member since:
    Sep 2007

    Posted 17 Nov 2010 Link to this post

    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));
        }
    }
  9. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 17 Nov 2010 Link to this post

    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>>
Back to Top
DevCraft banner