Steve
Steve

Reputation: 13

Linq to SQL using global datacontext to keep track of user changes?

I got a design dilemma and I'm not sure how to approach this as I'm new to the whole Linq scene. I'm planning on creating a winforms application using a datagridview to display some data from my database. There will be buttons and other things that the user can do to interact with the data and change it. But I'm not sure how to approach this, either have a global datacontext within my form that keeps track of the changes and then use the .SubmitChanges() to commit and have linq do everything behind the scene, or for every button event, I create a new datacontext but because linq can't track my changes anymore, I create a custom data structure of some sort to keep track of user changes and then update the db.

No matter what I think, it sounds like having a global datacontext makes it easier (coding-wise) but I was reading that I can run into problems?

There will be multiple users using the app at the same time connecting to a central database. I'm open to any suggestions, just not sure what the correct way of doing this.

Thanks!

Upvotes: 1

Views: 1216

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

In my ASP.NET application, we keep a global context per request, so the idea of a global context for the form isn't bad. Especially if you are saving interrelated data, data queried from multiple contexts, when you try to interrelate it, will cause an error. So a global context is beneficial from that perspective. Especially since windows forms can retain the object bound to it (unlike ASP since its stateless), these changes will sync up a lot more nicely.

The only downside you have to keep in mind is if you are loading a lot of data, a global context, if you are tracking entity changes, keeps all of those objects in memory, and the context could grow to a point where it could impact performance...

Upvotes: 1

Related Questions