How to save in database without committing

6 posts, 1 answers
  1. test
    test avatar
    27 posts
    Member since:
    Feb 2008

    Posted 10 Oct 2010 Link to this post

    Hi,

    I've a table with an identity column, when I insert a row in this table, I need to get the value of the identity column to continue my process, but I don't want to commit the identity column's value before the end of my process.

    When I use the SaveChanges() function commit is done. I see in "old posts" we can use the Flush() function to do updates on database without commiting but I think it seems to be with old versions of OpenAccess.

    So my question is : How can I do to solve my problem with the last version of OpenAccess ?

    Thanks for your help and advices.
  2. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 10 Oct 2010 Link to this post

    Hi test,

    You're right the Flush method is the way to go and you're also right that it is not part of the new Context API yet, but only available in the Classic approach.
    However, what you can do is to extend the generated Context class deriving from OpenAccessContext and then expose the scope in a public property like (pseudo code):

    public partial class MyScopeContext : MyContext
    {

       public IObjectScope GetObjectScope()
       {
           return this.GetScope();
       }
    }

    Each OpenAccessContext subclass has an underlying scope which you can access with the protected GetScope() method.
    Don't forget to create the new partial class in a new code file (.cs) so it doesn't get overwritten when you generate.

    So, to call Flush on a context you do:

    MyScopeContext.GetObjectScope().Flush();

    Regards

    Henrik
  3. DevCraft banner
  4. test
    test avatar
    27 posts
    Member since:
    Feb 2008

    Posted 11 Oct 2010 Link to this post

    Thanks  Henrik,

    The problem is : I don't find any method "flush" in IObjectScope interface, in fact I find it nowhere. Any idea ?
  5. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 11 Oct 2010 Link to this post

    Hi Test,

    Sorry... a typo.

    You will need to go to ObjectScope.Transaction.Flush();

    Be aware that Context.RevertChanges() will rollback the flushed changes if done after a ObjectScope.Transaction.Flush();

    Regards

    Henrik
  6. test
    test avatar
    27 posts
    Member since:
    Feb 2008

    Posted 11 Oct 2010 Link to this post

    ;-)

    I came back here to write the same things. I found Flush in Transaction.

    Thanks for your response and your speed.
  7. Answer
    TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 11 Oct 2010 Link to this post

    Hi test,

    Glad you (and I :-)) found the answer.

    Maybe you can mark the question (and post preferably also) as answered so that other users of the forum can find their answer quickly for a related question. Thanks.

    Regards

    Henrik
Back to Top
DevCraft banner