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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:26:38+00:00 2026-06-16T01:26:38+00:00

I am using N-layer architecure in my project. In the data access layer I

  • 0

I am using N-layer architecure in my project. In the data access layer I use Entity Framework code first.
When I fill an object in the DAL, dispose the context and transfer the object to the business layer, the navigation properties become null.
I have no problem if I don’t dispose the context, but my question is it good to dispose
the context?
If yes, how can I solve the problem when I transfer the object to the next layer (business), so that the navigational properties don’t become null.
My code in Data access layer:

public List<DomainObject.ContractCenter> GetAll()
{
    try
    {
        List<ContractCenter> contractCenters = new List<ContractCenter>();
        using (var context = new DBContext())
        {
            contractCenters = context.ContractCenters.ToList();
        }

        return contractCenters;
    }
}
  • 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-06-16T01:26:39+00:00Added an answer on June 16, 2026 at 1:26 am

    To answer your questions:

    is it good to dispose the context?

    Yes, the context should be as short-lived as possible (see @khellang’s answer regarding UnitOfWork and IOC if you have a possibility to restructure your project and implement cleaner architecture).

    how can i solve the problem when i transfer object to next layer

    You need to eager-load the related collections (navigational properties):

    public List<DomainObject.ContractCenter> GetAll()
    {
        try
        {
            using (var context = new DBContext())
            {
                return context.ContractCenters.
                               Include(c => c.YourChildCollection1).
                               Include(c => c.YourChildCollection2).
                               ...
                               ToList();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using DbProviderFactories in my data layer (based on Entity Framework) and am
I'm developing n-layer architecture, and for the data access layer I am using Entity
I have created a Data Access Layer using .NET. Everywhere that I update a
I'm building an e-commerce site using C#, MVC3, Entity Framework 4, my first stab
I am using Entity framworke in my project. I am using 3 layer architecture
Okay, here is my problem... I created a Data Layer using the RTM Fluent
I am using python's very high level layer to embed some python code to
So I'm about to create my first SPA project using .NET MVC4. But I
I have a ASP.net (C#) project that is using a three layer architecture. I
I am using a layered architecture with the Entity Framework as my datalayer with

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.