none
Linq to Sql and asp.net RRS feed

  • Question

  • Hello. I was looking around and read a lot of articles mentioning that the best practice to use Linq to Sql DataContext is to create a new object, use it, then close it.

    I'm developing a ASP.NET page and decieded to try linq to sql (haven't used it before for webpages). The thing is that I have a few pages that share some data and when you change from one page to another and would be saved in the db only when the "save" button was clicked.

    Now, I'm not sure the best way to proceed here. Could I create a static DataContext to hold the object reference until the info from the pages were saved and only then dispose the datacontext? Or is there a better way to do it? What's the best practice to keep alive a datacontext until user saves all changes and I can dispose it?

     

    Thanks

    Wednesday, May 18, 2011 5:39 PM

Answers

  • First off, you may want to look into Entity Framework (which is roughly the same (to a new end user)) but provides more power going forward. Regardless of whether you use linq to sql or EF I would not try to save a context between page calls, but pass along information in the standard ways (see How to: Pass Values Between ASP.NET Web Pages).

    Remember you want the pages to be as stateless as possible. One can save data to the database between calls and that is an option, but it requires you save the information and do the plumbing.

    As mentioned I would not hold an open context, it is best to not hold open such things, but to quickly dispose of them as soon as you open them to not have resource conflicts and other state problems holding between the calls.

    Note using the update panel you could simulate a page change by hiding the current work and going to the next item, thereby using one asp page, but mutiple work areas. Kind of like an accordian control...

    HTH


    William Wegerson (www.OmegaCoder.Com)
    • Marked as answer by Lucas Phillip Thursday, May 19, 2011 11:49 AM
    Wednesday, May 18, 2011 6:03 PM
    Moderator
  • Thanks for the reply Omega. I've already fixed some stuff  here (that I haven't mentioned) based on your reply.

    I'm still struggling with that "persistent" data. Use update panel or loading the entire asp.net wouldn't have the same effect? All the last page data is lost, and I would have to save the changes the user made and then load the next page. Please correct me if I'm wrong.

    I'm thinking about instead of storing the open datacontext, I could keep a reference to a table in a session and make all changes to that ref. Then, when the users finally wants to save his work, i'd create a new datacontext, replace the table with the one user was editing and then save to db.


    EDIT: just corrected a few typing errors to make the text clearer
    Wednesday, May 18, 2011 6:56 PM

All replies

  • First off, you may want to look into Entity Framework (which is roughly the same (to a new end user)) but provides more power going forward. Regardless of whether you use linq to sql or EF I would not try to save a context between page calls, but pass along information in the standard ways (see How to: Pass Values Between ASP.NET Web Pages).

    Remember you want the pages to be as stateless as possible. One can save data to the database between calls and that is an option, but it requires you save the information and do the plumbing.

    As mentioned I would not hold an open context, it is best to not hold open such things, but to quickly dispose of them as soon as you open them to not have resource conflicts and other state problems holding between the calls.

    Note using the update panel you could simulate a page change by hiding the current work and going to the next item, thereby using one asp page, but mutiple work areas. Kind of like an accordian control...

    HTH


    William Wegerson (www.OmegaCoder.Com)
    • Marked as answer by Lucas Phillip Thursday, May 19, 2011 11:49 AM
    Wednesday, May 18, 2011 6:03 PM
    Moderator
  • Thanks for the reply Omega. I've already fixed some stuff  here (that I haven't mentioned) based on your reply.

    I'm still struggling with that "persistent" data. Use update panel or loading the entire asp.net wouldn't have the same effect? All the last page data is lost, and I would have to save the changes the user made and then load the next page. Please correct me if I'm wrong.

    I'm thinking about instead of storing the open datacontext, I could keep a reference to a table in a session and make all changes to that ref. Then, when the users finally wants to save his work, i'd create a new datacontext, replace the table with the one user was editing and then save to db.


    EDIT: just corrected a few typing errors to make the text clearer
    Wednesday, May 18, 2011 6:56 PM
  • I recommend that you post your scenario to the asp.net forums so they can give you their take on the process. I am too much into Silverlight at this time to give justice to your thoughts. I like the idea of saving things to a table, then when save is hit creating a new context and extract and save; but pass it on to the asp.netters. :-) GL HTH


    William Wegerson (www.OmegaCoder.Com)
    Thursday, May 19, 2011 3:30 AM
    Moderator
  • Shall do it. Thanks for the help
    Thursday, May 19, 2011 11:50 AM