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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:00:22+00:00 2026-05-14T15:00:22+00:00

Through various questions I have asked here and other forums, I have come to

  • 0

Through various questions I have asked here and other forums, I have come to the conclusion that I have no idea what I’m doing when it comes to the generated entity context objects in Entity Framework.

As background, I have a ton of experience using LLBLGen Pro, and Entity Framework is about three weeks old to me.

Lets say I have a context called “myContext”. There is a table/entity called Employee in my model, so I now have a myContext.Employees. I assume this to mean that this property represents the set of Employee entities in my context. However, I assume wrong, as I can add a new entity to the context with:

myContext.Employees.AddObject(new Employee());

and this new Employee entity appears nowhere in myContext.Employees. From what I gather, the only way to find this newly added entity is to track it down hiding in the myContext.ObjectStateManager. This sounds to me like the myContext.Employees set is in fact not the set of Employee entities in the context, but rather some kind of representation of the Employee entities that exist in the database.

To add further to this confusion, Lets say I am looking at a single Employee entity. There is a Project entity that has a M:1 relationship with Employee (an employee can have multiple projects). If I want to add a new project to a particular employee, I just do:

myEmployee.Projects.Add(new Project());

Great, this actually adds the Project to the collection as I would expect. But this flies right in the face of how the ObjectSet properties off of the context work. If I add a new Project to the context with:

myContext.Projects.AddObject(new Project());

this does not alter the Projects set.

I would appreciate it very much if someone were to explain this to me. Also, I really want a collection of all the Employees (or Projects) in the context, and I want it available as a property of the context. Is this possible with EF?

  • 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-14T15:00:22+00:00Added an answer on May 14, 2026 at 3:00 pm

    An ObjectSet is a query. Like everything in LINQ, it’s lazy. It does nothing until you either enumerate it or call a method like .Count(), at which point a database query is run, and any returned entities are merged with those already in the context.

    So you can do something like:

    var activeEmployees = Context.Employees.Where(e => e.IsActive)
    

    …without running a query.

    You can further compose this:

    var orderedEmployees = activeEmployees.OrderBy(e => e.Name);
    

    …again, without running a query.

    But if you look into the set:

    var first = orderedEmployees.First();
    

    …then a DB query is run. This is common to all LINQ.

    If you want to enumerate entities already in the context, you need to look towards the ObjectStateManager, instead. So for Employees, you can do:

    var states = EntityState.Added || EntityState.Deleted || // whatever you need
    var emps = Context.ObjectStateManager.GetObjectStateEntries(states)
                                         .Select(e => e.Entity)
                                         .OfType<Employee>();
    

    Note that although this works, it is not a way that I would recommend working. Typically, you do not want your ObjectContexts to be long-lived. For this, and other reasons, they are not really suitable to be a general-purpose container of objects. Use the usual List types for that. It is more accurate to think of an ObjectContext as a unit of work. Typically, in a unit of work you already know which instances you are working with.

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

Sidebar

Related Questions

I have a single RoutedUICommand that can be accessed through various places in the
Through profiling I've discovered that the sprintf here takes a long time. Is there
Clicking through to the download-page if see that the last version of the download
Through some bungling in creating and removing publications, I was left with a lot
Through CommandName=Delete I try to delete a line from the ListView control but not
Through reflection, is there some way for me to look at a generic List's
Browsing through the git documentation, I can't see anything analogous to SVN's commit hooks
Moving through the maze forward is pretty easy, but I can't seem to figure
Reading through the Flickr API documentation it keeps stating I require an API key
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like

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.