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 77841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:53:37+00:00 2026-05-10T20:53:37+00:00

I have started using Linq to SQL in a (bit DDD like) system which

  • 0

I have started using Linq to SQL in a (bit DDD like) system which looks (overly simplified) like this:

public class SomeEntity // Imagine this is a fully mapped linq2sql class. {     public Guid SomeEntityId { get; set; }     public AnotherEntity Relation { get; set; } }  public class AnotherEntity // Imagine this is a fully mapped linq2sql class. {     public Guid AnotherEntityId { get; set; } }  public interface IRepository<TId, TEntity> {     Entity Get(TId id); }  public class SomeEntityRepository : IRepository<Guid, SomeEntity> {     public SomeEntity Get(Guid id)     {         SomeEntity someEntity = null;         using (DataContext context = new DataContext())         {             someEntity = (                 from e in context.SomeEntity                 where e.SomeEntityId == id                 select e).SingleOrDefault<SomeEntity>();         }          return someEntity;     } } 

Now, I got a problem. When I try to use SomeEntityRepository like this

public static class Program {     public static void Main(string[] args)     {         IRepository<Guid, SomeEntity> someEntityRepository = new SomeEntityRepository();         SomeEntity someEntity = someEntityRepository.Get(new Guid('98011F24-6A3D-4f42-8567-4BEF07117F59'));         Console.WriteLine(someEntity.SomeEntityId);         Console.WriteLine(someEntity.Relation.AnotherEntityId);     }  } 

everything works nicely until the program gets to the last WriteLine, because it throws an ObjectDisposedException, because the DataContext does not exist any more.

I do see the actual problem, but how do I solve this? I guess there are several solutions, but none of those I have thought of to date would be good in my situation.

  • Get away from the repository pattern and using a new DataContext for each atomic part of work.
    • I really would not want to do this. A reason is that I do not want to be the applications to be aware of the repository. Another one is that I do not think making linq2sql stuff COM visible would be good.
    • Also, I think that doing context.SubmitChanges() would probably commit much more than I intended to.
  • Specifying DataLoadOptions to fetch related elements.
    • As I want my Business Logic Layer to just reply with some entities in some cases, I do not know which sub-properties they need to use.
  • Disabling lazy loading/delayed loading for all properties.
    • Not an option, because there are quite a few tables and they are heavily linked. This could cause a lot of unnecessary traffic and database load.
  • Some post on the internet said that using .Single() should help.
    • Apparently it does not …

Is there any way to solve this misery?

BTW: We decided to use Linq t0 SQL because it is a relatively lightweight ORM solution and included with the .NET framework and Visual Studio. If the .NET Entity Framework would fit better in this pattern, it may be an option to switch to it. (We are not that far in the implementation, yet.)

  • 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. 2026-05-10T20:53:37+00:00Added an answer on May 10, 2026 at 8:53 pm

    Rick Strahl has a nice article about DataContext lifecycle management here: http://www.west-wind.com/weblog/posts/246222.aspx.

    Basically, the atomic action approach is nice in theory but you’re going to need to keep your DataContext around to be able to track changes (and fetch children) in your data objects.

    See also: Multiple/single instance of Linq to SQL DataContext and LINQ to SQL – where does your DataContext live?.

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

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You do have access to the Canvas, Grid or any… May 11, 2026 at 12:00 pm
  • added an answer I went for named pipes in the end. On the… May 11, 2026 at 12:00 pm
  • added an answer Try downsizing the images. Maybe they are too large and… May 11, 2026 at 12:00 pm

Related Questions

I have started using Linq to SQL in a (bit DDD like) system which
I have started using Jython as it seems to be a excellent language, and
I have started using aspects for parameter validation in our development framework. It works
Since I have started using this site, I keep hearing about the Boost library.
I have recently started using Vim as my text editor and am currently working
I have just started using silverlight 2 beta and cannot find how to or
I have just started using Boost 1.36. These libraries would be very useful in
I knew I should never have started using c++ io, the whole type safety
I have started testing my UI using qUnit, so I need to simulate some
I just started using SVN, and I have a cache directory that I don't

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.