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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:23:01+00:00 2026-05-27T03:23:01+00:00

Is there any particular reason why adding an item to an EF4.1 collection, saving

  • 0

Is there any particular reason why adding an item to an EF4.1 collection, saving it to the database and then selecting that collection out again would produce weird results?

When I do the initial load, all the items in the collection are of a type System.Data.Entity.DynamicProxies.MyClassName_LongString, so the following select on the collection works:

var y = MyCollection.Where(x => x.ValidTo == null).First();

Yes, there is always an item in the collection which fulfills that criteria. Always.

However, if I select the collection, add a new item to it, save the changes and then on the same context select the collection again, the last item in the collection (the new one), is not a dynamic proxy, but instead is of my POCO type (Moo.Model.MyClassName).

Carrying out the same select from above on that version of the collection throws a null reference exception – even though the last item (the non-dynamic proxy one) does indeed match the criteria, which I have confirmed through watching the collection manually…

Load the collection from another context after the changes and the behaviour doesn’t show itself – they are all dynamic proxies, and the select works.

Does anyone have any idea as to whats causing this behaviour?

  • 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-27T03:23:02+00:00Added an answer on May 27, 2026 at 3:23 am

    However, if I select the collection, add a new item to it, save the
    changes and then on the same context select the collection again, the
    last item in the collection (the new one), is not a dynamic proxy, but
    instead is of my POCO type (Moo.Model.MyClassName).

    If you create the new item with the new operator it is not a proxy. And it won’t become a proxy when you query it with the same context because Entity Framework checks if there is already an object with the same key attached to the object context and if yes, it doesn’t create a new (proxied) object. So, your new entity will still be the same object which is not a proxy.

    If you work this way you should actually create the entity as a proxy in the first place, which means that you don’t use the new operator but instead the Create method of DbSet<T>:

    var newEntity = dbContext.Entities.Create();
    

    Now, newEntity is a proxy like the other entities you already have in your collection.

    I don’t know though why you get the exception you described (which might have to do with the mix of proxy and non-proxy objects in the same collection) but would hope that it disappears when you use the Create method instead of new.

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

Sidebar

Related Questions

Is there any particular reason that all data members in a class are private
Is there any particular reason that it isn't in any of the the specs?
Presuming that your C++ compiler supports them, is there any particular reason not to
Is there any particular reason that the linking phase when building a project with
Is there any particular reason to use one over the other? I personally tend
Is there any way to check if a particular plugin is available? Imagine that
Is there any way to ensure that a class posts a particular NSNotification? (I
Is there any particular reason? Is it not possible at all or is it
Is there any particular reason why these are missing? They do exist in BigInteger
Is there any particular reason to use one over the other or it's all

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.