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

  • Home
  • SEARCH
  • 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 1064947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:44:22+00:00 2026-05-16T19:44:22+00:00

i started an ASP.net web project app to learn how EF4 can be used.

  • 0

i started an ASP.net web project app to learn how EF4 can be used. In my project i have defined 3 layers(DAL => Entity Framework 4, BLL => Business Logic Layer, UI).
The BLL and DAL share POCOs generated using the template feature of EF4.

For example i have “User” Poco class that looks like this:

 public partial class User
{
    #region Primitive Properties

    public virtual System.Guid Id
    {
        get;
        set;
    }

    public virtual string Name
    {
        get;
        set;
    }

    public virtual string Password
    {
        get;
        set;
    }

    public virtual string Email
    {
        get;
        set;
    }

    public virtual bool MarkedForDeletion
    {
        get;
        set;
    }

    #endregion
    #region Navigation Properties

    public virtual Role Role
    {
        get { return _role; }
        set
        {
            if (!ReferenceEquals(_role, value))
            {
                var previousValue = _role;
                _role = value;
                FixupRole(previousValue);
            }
        }
    }
    private Role _role;

    public virtual ICollection<Article> Articles
    {
        get
        {
            if (_articles == null)
            {
                var newCollection = new FixupCollection<Article>();
                newCollection.CollectionChanged += FixupArticles;
                _articles = newCollection;
            }
            return _articles;
        }
        set
        {
            if (!ReferenceEquals(_articles, value))
            {
                var previousValue = _articles as FixupCollection<Article>;
                if (previousValue != null)
                {
                    previousValue.CollectionChanged -= FixupArticles;
                }
                _articles = value;
                var newValue = value as FixupCollection<Article>;
                if (newValue != null)
                {
                    newValue.CollectionChanged += FixupArticles;
                }
            }
        }
    }
    private ICollection<Article> _articles;

    public virtual Status Status
    {
        get { return _status; }
        set
        {
            if (!ReferenceEquals(_status, value))
            {
                var previousValue = _status;
                _status = value;
                FixupStatus(previousValue);
            }
        }
    }
    private Status _status;

    #endregion
    #region Association Fixup

    private void FixupRole(Role previousValue)
    {
        if (previousValue != null && previousValue.Users.Contains(this))
        {
            previousValue.Users.Remove(this);
        }

        if (Role != null)
        {
            if (!Role.Users.Contains(this))
            {
                Role.Users.Add(this);
            }
        }
    }

    private void FixupStatus(Status previousValue)
    {
        if (previousValue != null && previousValue.Users.Contains(this))
        {
            previousValue.Users.Remove(this);
        }

        if (Status != null)
        {
            if (!Status.Users.Contains(this))
            {
                Status.Users.Add(this);
            }
        }
    }

    private void FixupArticles(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (Article item in e.NewItems)
            {
                item.Author = this;
            }
        }

        if (e.OldItems != null)
        {
            foreach (Article item in e.OldItems)
            {
                if (ReferenceEquals(item.Author, this))
                {
                    item.Author = null;
                }
            }
        }
    }

    #endregion
}

Because all the properties are marked with virtual EF4 should create a proxy class in order to access all data of the POCO. Also i have enabled lazy loading for the db context.

When i want to display a users details i have the fallowing scenario:

  • UI=> instantiates a class from BLL (UsersManager) and calls the method GetUserByEmail(string email) that returns a User.

  • BLL=> in the method GetUserByEmail(string email) of the type UsersManager i instantiate a class from DAL(UsersDataManager) and call a method GetUserByEmailFromDAL(string email) that returns a User.

  • DAL=> in the GetUserByEmailFromDAL(string email) i instantiate a context, query for the user and return it.

the problem is that because only the DAL knows about the context and it is disposed after it exits the function the POCOs navigation relationships are Nulled and i don’t have acces to any of their properties.

if i do myUser.Role.Name i get an error like this:

Role = 'user1.Role' threw an exception of type 'System.ObjectDisposedException'
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

in order to bypass the problem i decided to modify my methods in order to tell it them to eager load the navigation properties when i need them. After this modification my method in DAL looks like this:

 public User User(string email, bool loadRelationships)
        {
            User user = null;
            if (!loadRelationships)
            {
                user = (from p in dbContext.Users where p.Email.Equals(email) select p).FirstOrDefault<User>();
            }
            else {
                user = (from p in dbContext.Users.Include("Role").Include("Status") select p).FirstOrDefault<User>();
            }

            return user;
        }

with this modification a problem still exists..i don’t have access to the related entities navigation entities… so for instance if a Role type had a Collection of permissions if i wanted to something like this:

User user1 = UserManager("test@test.com");
foreach(Permission perm in user1.Role.Permissions)
Console.WriteLine(perm.Name); => here i'd get an error like the one mentioned earlier.

Lazy loading works when the Poco is attached to a db context. Is there a mechanism or strategy that can be used to load navigation properties like in my scenario?

thank you.

  • 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-16T19:44:22+00:00Added an answer on May 16, 2026 at 7:44 pm

    Your traditional Presentation/Business/Data three-tier architecture doesn’t lend itself to lazy-loading. The UI should never directly cause a query to execute.

    If you want your UI layer to be able to lazy-load related entities, you’d have to surface the context from the DAL through your business layer. This might not be a good idea, because if you allow this you can inadvertently end up with queries in the UI that load a lot more data that you intend. You also break your separation of concerns.

    Your method above of loading related data on demand by parameter is a better approach.

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

Sidebar

Related Questions

No related questions found

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.