Link to home
Start Free TrialLog in
Avatar of scottsanpedro
scottsanpedro

asked on

Datacontext and classes

Hi, I have been banging my head on this issue for a bit of time now.
I have a vb.net windows form application and I'm attempting my first discovery of linq to sql. Using sql server 2005.
To simplify I have a main form with fields. Address1 address2 etc
I have a class names clsCompany with has the logic to run a query and retrieve records via linqtosql. I then pass the table back to the main form via a property and then bind it to a bindingsource. No great shakes yet.
I then update some fields and want to run db.submitchanges.
Now the datacontext cannot be set as global as the data will go stale.
So the question I really need answering.
What is the recommended way for me to set this model up.
Draw the data via the first datacontext. Let it go out of scope and then on update create a new one and pass the fields independently?
OR
Actually keep the datacontext global/live?
OR
????
The first issue obviously losses the change tracking ability etc
REALLY CONFUSED
Thanks in advanced Scott

Also refer to
https://www.experts-exchange.com/questions/23282867/Linq-DataContext-best-practices.html?sfQueryTermInfo=1+10+datacontext
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

I tend to create a datacontext on each form where i use the linq classes. A global datacontext would be a disaster.
Avatar of scottsanpedro
scottsanpedro

ASKER

thanks for answering.
So do you keep the datacontext in scope all the time within the form?
I have since created a management class that has only a shared datacontext.
When searching a new record I use this each time. When saving a record I use the 'USING' syntax around the db.submitchanges() to kill the context off, then re-search on the same record to bring it back live.
Not sure if this is overkill to keep the datacontext fresh?
Scott
Public Class clsCompanyContext
    Public Shared mCompanyContext As CompanyControlDataContext

    Public Sub New()
        mCompanyContext = New CompanyControlDataContext
    End Sub

End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I believe I was looking for confirmation that my approach was correct and feel I received that.
Many thanks for your time
Scott