Can I safely mix Linq-to-SQL queries and non-Linq-to-SQL queries in the same DataContext.Connection?

Suppose I have code in this form:

using (var dc = new MyDataContext()) {
    // ...code that may or may not look up objects via Linq-to-SQL...

    // ...code that may or may not *update* objects via Linq-to-SQL...
    // and call SubmitChanges...

    // Non-Linq-to-SQL code:
    dc.Connection.Open(); // <== I believe I need to do this
    using (SqlCommand cmd = dc.Connection.CreateCommand()) {
        // ...set up the command...
        using (SqlDataReader reader = cmd.ExecuteReader()) {
            // ...use the reader here...
        }
    }

    // ...more code that may or may not look up objects via Linq-to-SQL...

    // ...more code that may or may not *update* objects via Linq-to-SQL...
    // and call SubmitChanges...
}

Is it safe, for example, am I allowed to co-opt the connection this way? Should I call correctly Openif the code above was not supposed to make any DB calls?

This MSDN page seems to say that this is normal:

DataContext is the main channel through which you connect to the database, extract objects from it and send the changes back. You use the DataContext the same way you used the ADO.NET SqlConnection.

(, DataContext. , .)

, .Net 3.5 .

+4
1

, . , , , LINQ-to-SQL . , - , .

LINQ-SQL , using TransactionScope.

Entity Framework, . , , LINQ-to-SQL - /, .

+3

Source: https://habr.com/ru/post/1598506/


All Articles