Force the Opening of the DataContext's Connect

2019-07-27 12:50发布

问题:

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

回答1:

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