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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:16:13+00:00 2026-05-31T04:16:13+00:00

In my DALs I currently use a new DataContext instance for each method, i.e.

  • 0

In my DALs I currently use a new DataContext instance for each method, i.e. create the context for each data call, then dispose it (with using). I remember I read that was sort of a best practice.

Now I think that I probably better use one common DataContext per DAL which will require less lines to write and will allow to update changes in the database without attaching the entities to the newly created context.

But I am not sure whether this will impact the productivity of the application. Are there negative things which may appear with this new approach, like maybe “each context reserves a connection line with a database” or “there are only a limited number of contexts available per application“?

  • 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-31T04:16:14+00:00Added an answer on May 31, 2026 at 4:16 am

    From what I read and my own conclusion, the basic rule is: use a single DataContext instance for each short time set of operations, this means:

    1. Use new (separate) instance of DataContext for each operation (transaction) in long living parent objects, such as DALs. For example, the main form has a DAL which uses a DataContext, the main form is the most long living object in a desktop application, thus having a single instance of a DataContext to serve all the main form data operations will not be a good solution due to the increasing cache and risk of the data to become obsolete.

    2. Use single (common) instance of DataContext for all operations in short time living parent objects. For example, if we have a class which executes a set of data operations in a short amount of time, such as takes data from a database, operates with them, updates them, saves the changes to the database and gets disposed, we better create one single instance of the DataContext and use it in all the DAL methods. This relates to a web applications and services as well since they are stateless and are being executed per request.

    Example of when I see a requirement of a common DataContext:

    DAL:

    // Common DAL DataContext field.
    DataContext Context = new DataContext();
    
    public IEnumerable<Record> GetRecords()
    {
        var records = Context.Records;
    
        foreach (var record in records)
        {
            yield return record;
        }    
    }
    
    public void UpdateData()
    {
        Context.SaveChanges();
    }
    

    BLL:

    public void ManageData()
    {
        foreach (var record in DAL.GetRecords())
        {
             record.IsUpdated = true;
             DAL.UpdateData();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently maintaining a small-medium sized java web application (using only plain JSPs/Servlets) that
I am creating an MVC application framework, using only libararies and components that I
I am developing an asp.net website using typed dataset as DAL , everything works
I want to execute a SQL Server stored procedure using C#. This is my
Let me preface this question and state that using Entity Framework is not an
I am using ASP.Net / WebForms / Entity Model / Framework 3.5 Here is
I am new to MVC and dependency injection. Please help me to understand how
I have a (DAL) Data Access Layer (but this question is relevant for DAOs
What are good ways of achieving this DB agnosticism without actually coding two DAL's?
I'm looking for any database framework/library for .net which will act as a unified

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.