Note that there are some explanatory texts on larger screens.

plurals
  1. POLinqToSql declare and instantiate DataContext best practice?
    primarykey
    data
    text
    <p>What's the best practice in terms of setting up my DataContext for easy access in my extended LinqToSql classes?</p> <p>For example, I have a "User" entity in my dbml and I want to add methods to that class like so:</p> <pre><code>Partial Public Class User Public Function GetUser(ByVal UserID as Integer) as User 'Do Work End Function End Class </code></pre> <p>In order to access my DataContext I'd have to declare it inside the method like so:</p> <pre><code>Partial Public Class User Public Function GetUser(ByVal UserID as Integer) as User Dim dc as New MyDataContext() Return (From u in dc.Users Where u.ID = UserID).Single() End Function End Class </code></pre> <p>I'd like to not have to do that for every single method. Normally (if I weren't extending the LinqToSql dbml classes) I could just do this:</p> <pre><code>Partial Public Class User Private dc as MyDataContext Public Sub New() dc = new MyDataContext() End Sub Public Function GetUser(ByVal UserID as Integer) as User Return (From u in dc.Users Where u.ID = UserID).Single() End Function Public Function GetAllUsers() as IEnumerable(Of User) Return From u in dc.Users End Function 'etc... End Class </code></pre> <p>This would allow me to access the datacontext for each method without having to declare it newly each time. But of course you can't do that because the dbml already has a constructor. And adding code into the dbml always gets overwritten if anything ever changes.</p> <p>Anyone have any good ideas on how to save myself some excess code here?</p> <p>TIA!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload