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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:04:53+00:00 2026-05-22T22:04:53+00:00

I’m getting some strange behaviour from EF Code First when I add an object

  • 0

I’m getting some strange behaviour from EF Code First when I add an object to the database and select it back from the database in the same HTTP request.

When I retrieve it, it is returned as the object type rather than of type System.Data.Entity.DynamicProxies so the lazy loading doesn’t work.

If I retrieve the same object in the next HTTP Request then it is return as type System.Data.Entity.DynamicProxies and the lazy loading works fine.

Do I need to do something to the object or the context to get a DynamicProxy returned? Or is this how EF Code First is meant to work, if so how do I get around this issue?

I’ve even recreated the problem using the sample code in the NuGet packages.

Here’s the code that I used to recreate the problem:

public ActionResult Index() {
    var blogContext = new BlogContext();

    var posts = blogContext.Posts;

    ViewBag.Message = "Welcome to ASP.NET MVC!";
    ViewBag.Posts = posts;

    return View();
}

public ActionResult AddPost() {
    var blogContext = new BlogContext();

    var post = new Post() {
        PublishDate = DateTime.Now,
        Text = "Text",
        Title = "Title",
    };

    blogContext.Posts.Add( post );

    blogContext.SaveChanges();

    var post2 = blogContext.Posts.Find( post.ID );

    return RedirectToAction( "Index" );
}

public ActionResult GetPost(int id) {
    var blogContext = new BlogContext();

    var post = blogContext.Posts.Find( id );

    ViewBag.Post = post;

    return View();
}

When I get post2 in the AddPost action it is return of type MvcApplication.Models.Post where as when I return the same post in the GetPost action it is of type System.Data.Entity.DynamicProxies.Post.

The above code was recreated using the default MVC3 Application from the MVC 3 Tools Update I also installed the following NuGet packages: EntityFramework.Sample and EntityFramework.SqlServerCompact.

If anyone can recreate the problem or has a solution for this then it would be greatly appreciated.

  • 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-22T22:04:54+00:00Added an answer on May 22, 2026 at 10:04 pm

    In C#, if you call new Post(), then you’re going to get a Post instance, not an instance of a proxy subtype of Post. To get a proxy, you have to call something different.

    You can, e.g., call DbSet<T>.Create:

    var post = blogContext.Posts.Create();
    post.PublishDate = DateTime.Now;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.