473,320 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Single Linq Context in WinForms application

For winforms application with multiple related forms it is reasonable to
create Linq database context object in start of application.
Context object is released only when application exits.

So connection to database may remain open for all application run time.
For such a long time, some routers close connection. This causes Connection
is broken error in application.

How to fix this ?

Andrus.
Jan 13 '08 #1
4 4653
Andrus,

Generally speaking, a context is not meant to be open for the life of an
application. It implies that you want to keep track of all the objects that
were ever created, or ever will be created in the scope of your app, and
generally, that is way too broad.

Rather, when you have a specific group of operations that you are going
to perform, you should create a new instance of the context, use it, and
then dispose of it. Your app is not one giant context, rather, you have
multiple sub-functions which really are their own contexts. You need to
identify these and then work from there.

Also, I seriously doubt that the context is keeping the database
connections open. That's just horrible practice. While I am not positive,
I am guessing that there is some connection pooling going on and that those
connections are what are actually being dropped. The actual connections in
code are closed correctly. Keeping them open would provide no benefits to
the context.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrus" <ko********@hot.eewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
For winforms application with multiple related forms it is reasonable to
create Linq database context object in start of application.
Context object is released only when application exits.

So connection to database may remain open for all application run time.
For such a long time, some routers close connection. This causes
Connection
is broken error in application.

How to fix this ?

Andrus.
Jan 13 '08 #2
Also, I seriously doubt that the context is keeping the
database connections open.
Actually, based on the other problems Andrus has seen with DbLinq
(i.e. not LINQ-to-SQL), it wouldn't surprise me in the least...

Marc
Jan 14 '08 #3

Nicholas,
Generally speaking, a context is not meant to be open for the life of
an application. It implies that you want to keep track of all the objects
that were ever created, or ever will be created in the scope of your app,
and generally, that is way too broad.

Rather, when you have a specific group of operations that you are going
to perform, you should create a new instance of the context, use it, and
then dispose of it. Your app is not one giant context, rather, you have
multiple sub-functions which really are their own contexts. You need to
identify these and then work from there.
I'm creating multi form winforms application working with server over
internet.
I need to cache data so that every form does not read data from database.

Only way in Linq to do this is use Context object.
So I need to use single global context in application as global cache.

However I have no idea how to edit data for some form.

Should I create new context for every form for data editing and keep this
context open until form is closed ?
If so how to refresh global context when changes to form temporary context
are submitted ?

Andrus.
Jan 14 '08 #4
Andrus,

Caching the data on the app level is just too broad. Generally, if your
individual forms are going to handle a single operation (not a single data
operation, but it is a logical context), then you should load the data when
the form opens and close the context when the form is closed (or saved,
depending on the workflow of your app).

Generally, you cache data which is not going to be modified very often.
That's the whole point of caching. If you were to keep a cache using the
context of all the items in the database, you are going to have a nightmare
when it comes to concurrency checking, since you would probably have to
re-load the data anyways anytime you open a new form (depending on the
frequency of changes).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrus" <ko********@hot.eewrote in message
news:O9**************@TK2MSFTNGP04.phx.gbl...
>
Nicholas,
> Generally speaking, a context is not meant to be open for the life of
an application. It implies that you want to keep track of all the
objects that were ever created, or ever will be created in the scope of
your app, and generally, that is way too broad.

Rather, when you have a specific group of operations that you are
going to perform, you should create a new instance of the context, use
it, and then dispose of it. Your app is not one giant context, rather,
you have multiple sub-functions which really are their own contexts. You
need to identify these and then work from there.

I'm creating multi form winforms application working with server over
internet.
I need to cache data so that every form does not read data from database.

Only way in Linq to do this is use Context object.
So I need to use single global context in application as global cache.

However I have no idea how to edit data for some form.

Should I create new context for every form for data editing and keep this
context open until form is closed ?
If so how to refresh global context when changes to form temporary context
are submitted ?

Andrus.

Jan 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Veeneman | last post by:
I've been hearing a lot about LINQ in connection with Orcas, the next release of VS.NET. Micorosoft touts LINQ as the Next Big Breakthrough, but it looks to me like further muddying of application...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
2
by: Andrus | last post by:
I'm creating DbLinq MDI WinForms application. It allows edit customers list in DataGridView. Every row change is submitted separately. As Nicholas recommends, it is reasonable to create database...
1
by: VictorLeo | last post by:
I have an application that this throws with VS 2008 and LINQ everything works well, the problem is when I try to create an installer for this project. Adding a project to install add the main...
3
by: TisMe | last post by:
Hi All, I am using Linq to SQL dataclass for my website application. This was working AOK, but then I deleted and recreated the dataclass (Seemed the simpliest way of updating the class to...
0
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
*background* I've got an application that I'm creating that is using LINQ to SQL. We have a nice DataContext that is able to do lots of nice updating. But we are using something like an MVC model...
1
by: Dean Slindee | last post by:
VS2008, .NetFramework 3.5 SP1: I have built a LINQ data access layer project. When the LINQ data context was built over an existing SQL2005 database, the connection string for that database was...
1
by: psycho | last post by:
How do we return a single value from a stored procedure. Suppose I have a stored procedure like this: create proc dbo.spInsertGroup @ID uniqueidentifier @GroupName varchar(100), @IsActive...
1
balabaster
by: balabaster | last post by:
Does anyone have experience integrating LINQ into SOA architecture. I've spent all night trawling through documents and books regarding this and I've come to the conclusion that I'm going about...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.