Answered When I dispose the DbContext, how I can save changes later ?

  • Tuesday, June 05, 2012 9:21 AM
     
     

    I know that I should dispose the DbContext as soon as possible. but I have a TreeViewModel where each node is a an ItemViewModel that query MyDbContext to load data when IsExpanded is set to true. let's say that after the expand, I disposed MyDbContext, so when the user want to expand another node, an error will occure...

    the TreeView should be always visible in my Descktop Application, so he can browse the Products, rename them and change prices...

    so, Should I dispose MyDbContext after each operation in this scenario or what ?


    AAK

All Replies

  • Tuesday, June 05, 2012 12:58 PM
     
     

    hello,

    If we consider an MVC pattern... TreeView is a view that has no knowledege/concern of the underlying model or persistence layer.

    So, imho, you should design a controller that feeds your view with data on demand. Such controller create and dispose context ar whatever he needs.

    By the way, your description of the application makes me think to an Excel WorkSheet: good for a single user. If you have no more than one user... let the context live :)

    Sincerely yours

    thierry


    thierry

  • Tuesday, June 05, 2012 6:46 PM
     
     
    thanks for the reply. Sorry I am new to this, what you mean by single user ? any desktop application is for a single user or not ? how can many users use the same application ?

    AAK

  • Tuesday, June 05, 2012 7:06 PM
     
     Answered
    On 6/5/2012 5:21 AM, Ahmad El Kerdi wrote:
    > I know that I should dispose the DbContext as soon as possible. but I
    > have a TreeViewModel where each node is a an ItemViewModel that query
    > MyDbContext to load data when IsExpanded is set to true. let's say that
    > after the expand, I disposed MyDbContext, so when the user want to
    > expand another node, an error will occure...
    >
    > the TreeView should be always visible in my Descktop Application, so he
    > can browse the Products, rename them and change prices...
    >
    > so, Should I dispose MyDbContext after each operation in this scenario
    > or what ?
    >
    If this is a fat-client application where the entire application,
    database and everything else is local to the user's machine, then you
    can level the database connection open and close the connection when the
    user exits the program. No one else but the user on the machine is using
    the application and the database.
     
    If this is a thin-client application where multiple users on different
    machines are using the same database remotely, then you need to open and
    close connections with the database when access is completed.  In doing
    that, it doesn't block other users from using the remote database.
     
  • Tuesday, June 05, 2012 9:00 PM
     
     
    aha.... I think that I should read more about this, thanks a lot, I think that my application will be a fat-client application. This is the first time that i know that in this scenario, I can leave the connection open ! thanks a lot...

    AAK