views:

233

answers:

1

Hi,

When you create a datacontext, its connection is closed until you retrieve objects and it stays open when you retrieve objects in case you use deferred operators or late binding.

Is it possible (in an extension method of the datacontext of not) to force the datacontext to open its Connection without querying LINQ with LINQ or doing a fake query to the database with ExecuteQuery, ExecuteCommand, etc?

Thanks

A: 

You can use the DataContext.Connection property to retrieve the connection, and call Open() yourself.

Jon Skeet
I'll try but will it put the datacontext in an invalid state? Maybe the member functions of the datacontext open the connection if it's not already open, without setting any other flag.
Mike Gleason jr Couturier
@Mike: I don't know the details of this, to be honest. I *believe* it is legal though.
Jon Skeet
I've taken another approach.. I'll post my article here when done... I'l mark you answer as accepted though.
Mike Gleason jr Couturier
Here's what I did (I first tried to use the DataContext's Connection, that's the reason behind this question): http://blog.mikecouturier.com/2010/01/sql-2008-tvp-table-valued-parameters.htmlYou're a hell of an active guy ;)
Mike Gleason jr Couturier