delete an entity without actually load it into the context

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

    Posted 27 Oct 2010 Link to this post

    Hi,

    is there a method to execute e database delete without the need to load the object into the db context  before calling DbCtx.Delete(obj)? By now what I do is:

    1) get the object key from a grid/list
    2) query/load the object from the dbcontext
    3) perform a context.Delete(obi)
    4) perform a context.SaveChanges() to persist the delete

    In web application is a day-by-day task to bind grids  to object lists and perform a delete operation querying the object key from the grid itself. performing steps 1->4. is a huge waste of resources (and processing time).

    best regards
    Andrea
  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 29 Oct 2010 Link to this post

    Hello AndreaT,

    I am afraid that there is no other approach that you can use in this scenario. However, if the object has already been loaded in order to be shown in the grid, it is cached in the context instance used to retrieve it. So in most cases when you try to delete it, it will not be read again from the database but from the cache. Hope that helps.

    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
  3. DevCraft banner
  4. AndreaT
    AndreaT avatar
    27 posts
    Member since:
    Sep 2007

    Posted 31 Oct 2010 Link to this post

    Hi,

    unfortunately in a web application the context is wiped out at the end of the page life cycle. Performing delete preloading the object is a havy duty (and unnecessary) job. It would be better to have a direct method to perform deletions.

    thank you

    Andrea
  5. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 02 Nov 2010 Link to this post

    Hi AndreaT,

    I think the only other solution you could use is to call a stored procedure which deletes the record from the database. Here are the steps you should follow:
    1. Create a stored procedure on the sql server which can delete an object using its primary key;
    2. Reverse map the stored procedure using the Update from Database wizard;
    3. Execute the method which is mapped to the stored procedure:

    ...
    context.StoredProcedureMethod(id);
    ...

    Hope that helps.

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

    Posted 03 Nov 2010 Link to this post

    Hi AndreaT and Damyan,

    Sorry for interfering your thread, but another solution could be to enable the L2 cache. Especially since (as per the scenario AndreaT described) a list of objects are loaded initially and thus they are already present in the L2 cache.

    So, as the L2 cache lives next to the L1 cache in the Scope/Context and never dies when a Scope/Context dies it will be a nice solution I guess.

    Since the object is already loaded during display in web app, fetching it for delete will not hit the database (but go to the L2 cache for the object) and thus it can be deleted afterwards.

    Just my thoughts,

    Regards

    Henrik
  7. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 05 Nov 2010 Link to this post

    Hi Andrea,

    Henrik is right, you can try the level 2 cache as well. Helpful information regarding the caching mechanisms in OpenAccess ORM can be found here, here and here. This help article demonstrates how to set the caching mechanism for your domain model.

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