Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3669082
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:12:01+00:00 2026-05-19T02:12:01+00:00

How do you use LINQtoSQL with the repository pattern? I’m new to L2S and

  • 0

How do you use LINQtoSQL with the repository pattern?

I’m new to L2S and find its lazy loading to be a real impediment to using the repo pattern.

Usually, I think of the repository pattern like this:

var myCustomer = null;

using (var myRepo = new Repo()){
    myCustomer = myRepo.GetCustomerForCustomerId(123);
}

if(myCustomer.Orders.Any()){
 //do something
}

Trouble is, won’t L2S attempt to make a data connection when myCustomer.Orders is interrogated? Doesn’t this lead to unpredicatable database access issues?

I mean, yes, I could tell my repo to verify orders inside of the repo confident that our complete test coverage verifies that developers never call an entity we didn’t explicitly load, but I would rather just get rid of the lazy loading/object-datacontext persistence.

So I have 4 options

  1. Create domain objects that get created from the L2S objects – lots of work and maintainance
  2. Create derived verisons of my L2S object that break he linkage (http://www.codeproject.com/KB/linq/linq-to-sql-detach.aspx)
  3. Use LLBLGenPro instead.
  4. Appeal to the wisdom of stack overflow readers

I’m going with 4 for now.

How do I ensure that my objects wont call the db after my repo is closed?

And yes, I did read every stack question that talks about L2S and Repos and none of them answer this question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:12:02+00:00Added an answer on May 19, 2026 at 2:12 am

    Sometimes it is useful to eager-load an entity’s children. You can do so with DataLoadOptions.LoadWith which tells your DataContext to automatically load an entity’s children when the parent entity is loaded.

    Snippet from the MSDN link above:

    Northwnd db = new Northwnd(@"c:\northwnd.mdf");
    DataLoadOptions dlo = new DataLoadOptions();
    dlo.LoadWith<Customer>(c => c.Orders);
    db.LoadOptions = dlo
    

    You can also use DataLoadOptions.AssociateWith to further customize the behavior of the automatic loading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am integrating SqlCacheDependency to use in my LinqToSQL datacontext. I am using an
I use Repository pattern (and linq2sql as data access) and have, for example, ProductsRep
I am about to use linqtosql in my first asp.net mvc application. I have
use Text::Table; my $tb = Text::Table->new(Planet,Radius\nkm,Density\ng/cm^3); $tb->load( [ Mercury,2360,3.7], [ Mercury,2360,3.7], [ Mercury,2360,3.7], );
I'm using LINQtoSQL to create cascading deletes IE: if Category is deleted then all
In asp.net(c#),i use linqtosql,In the table,i 've set property of a field Isunique to
I've written some really nice, funky libraries for use in LinqToSql. (Some day when
I have inherited a LinqToSql application which is making use of GUID keys for
I'm using the LinqtoSQL for a WPF M-V-VM application, as I might want to
I am trying to figure out how to use the new EF Code First

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.