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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:37:05+00:00 2026-05-29T06:37:05+00:00

I am fairly new to .NET C# development and recently started using LINQ to

  • 0

I am fairly new to .NET C# development and recently started using LINQ to SQL for the majority of my data access layer. Unfortunately, I have been struggling with how to manage my DataContext so that I don’t get the dreaded exceptions that result from entities either not being attached to a context or attempting to attach an entity to one context while it is attached to another. After much reading, I believe that the best solution for my application is to leave the DataContext open for the entire duration of my application.

In short, I am using the Repository pattern to implement the basic CRUD operations (i.e. Create, Read, Update, and Destroy) for all entities managed by my context. My implementation of the Repository is included below. In addition to the Repository, I have DAOs (Data Access Objects) for each entity that has more specific data access methods (i.e. CustomerDAO.getCustomerByName(string name), etc…). Each of my Windows forms has its’ own instance of one or more DAOs (that extend Repository) and the DataContext in my repository is static. The problem that I am running into is that even though my DataContext in the repository class below is declared as static, I’m finding that each distinct DAO actually gets a different instance of the DataContext. For example, if I have 8 references to the CustomerDAO, they all have the same DataContext. But, if I create one WorkOrderDAO I get another instance of the DataContext and all future instances of WorkOrderDAO get this same DataContext. More specifically, I discovered this in the following scenario.

1) Use an instance of WorkOrderDAO to load all WorkOrders into ListView – Has one DataContext
2) Use an instance of WorkOrderJobsDAO to attempt to delete one of the jobs on the WorkOrder. This is a collection on the WorkOrder. Has a different DataContext so I can’t attach

Is this a problem with how I have implemented the Repository below? The only thing that I can think of to solve this issue is to create a Singleton that the Repository uses to get its’ DataContext. Can anyone make any recommendations here for how I should manage the Context?

public class Repository<T> : IRepository<T>
where T : class
{
    private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
    protected static DomainClassesDataContext db = new DomainClassesDataContext();
    private static bool dataContextOptionsInitialized = false;

    public Repository()
    {
        if (!dataContextOptionsInitialized)
        {
            db.DeferredLoadingEnabled = true;
            dataContextOptionsInitialized = true;
        }
    }
    public void AddEntity(T entity)
    {
        GetTable.InsertOnSubmit(entity);
        SaveAll();
    }

    public void DeleteEntity(T entity, bool attach)
    {
        if(attach)
            GetTable.Attach(entity);

        GetTable.DeleteOnSubmit(entity);
        SaveAll();

    }

    public void UpdateEntity(T entity, bool attach)
    {
        if(attach)
            GetTable.Attach(entity, true);
        SaveAll();

    }

    public System.Data.Linq.Table<T> GetTable
    {
        get { return db.GetTable<T>(); }
    }

    public IEnumerable<T> All()
    {
        return GetTable;
    }

    public void SaveAll()
    {
        db.SubmitChanges();

    }
}
  • 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-29T06:37:06+00:00Added an answer on May 29, 2026 at 6:37 am

    Generic classes in C# gets “expanded” on compilation, so your Repository will be a different class than Repository, which is why the static variables are different instances between your DTOs. So yes you probably want to save it somewhere else, like a in a Singleton class.

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

Sidebar

Related Questions

I'm fairly new to asp.net and especially LINQ and SQL. Say I have a
I am fairly new in asp.net development, so you can say that I have
I'm still fairly new to ASP.NET development so bear with me. I'm going to
I am fairly new to ASP.NET and I discovered repeaters recently. Some people use
I am fairly new at using the ASP.NET MVC framework and was hoping that
I have an issue with .Net reflection. The concept is fairly new to me
I'm fairly new to using AJAX in ASP.NET (although I am very familiar with
I am fairly new to asp.net, and have little experience with iis. I would
I am fairly new to .NET and C#, but I have a DataGridView that
I'm fairly new to .Net... I have several queries which will execute when the

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.