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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:40:35+00:00 2026-06-18T11:40:35+00:00

After switching a project from the EntityObject generator to DbContext, I ran into an

  • 0

After switching a project from the EntityObject generator to DbContext, I ran into an issue using some navigation properties on new objects. I’ve spent a significant amount of time researching the problem, and I’m no closer to a desirable solution.

First, the generated class definitions:

public partial class Category
{
    public Category()
    {
        this.Limits = new HashSet<Limit>();
    }

    public int CategoryId {get; set;}
    public string Name {get; set;}
    public virtual ICollection<Limit> Limits { internal get; set; }
}

public partial class Limit
{
    public int CategoryId {get; set;}
    public string Description {get; set;}
    internal virtual Category Category { get; set; }
}

I am creating test data during an integration test using the following code:

using (GFCAMDataContext db = new GFCAMDataContext())
{
    limit = new Limit()
    {
        CategoryId = testData.CategoryId,
        Description = "SignerController.Update"
    };

    db.Limits.Add(limit);
    db.SaveChanges();
}

Without any other changes, the Limit.Category property of my newly-created Limit object does not return anything. However, if I query the desired Category from the DbContext before SaveChanges is called the navigation property on my new Limit starts returning the associated Category. With ObjectContext, the Category property is updated without any intervention from me.

I would like to have the same behavior as ObjectContext, but I can’t seem to find a way to achieve this goal. I’ve seen a couple of proposed solutions:

  • Make the navigation properties public. This had the same behavior, and isn’t desirable as the public navigation properties can cause issues during serialization and aren’t needed outside of my business layer.

  • Make all properties public virtual and use DbSet.Create to ensure proxy creation. This resulted in the same behavior, and isn’t necessarily desirable as I have code dynamically creating instances (i.e. I don’t have access to a DbSet at the time I am creating an entity instance).

Does anyone have any suggestions for a solution to this problem?

  • 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-18T11:40:36+00:00Added an answer on June 18, 2026 at 11:40 am

    One solution would be to explicitly load the nested entity:

    db.SaveChanges();
    db.Entry(person).Reference(z => z.Category).Load();
    

    The other option, when proxies are enabled, is indeed to call DbSet.Create<T>(). If you don’t have access to the DbSet instance at the time you create your entities, you might want to expose a public method in your repository interface that allows that. For example:

    public interface IRepository<T>
    {
        T Add(T entity);
    
        T GetById(...);
    
        void SaveChanges();
    
        ...
    
        T CreateInstance(); // Concrete implementation have access to DbSet and uses DbSet.Create<T>()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After switching from firefox testing to internet explorer testing, some elements couldn't be found
I am currently switching from developing two separate Xcode Projects into using Xcode workspace.
After switching from Delphi2007 to XE2, I'm terribly unhappy with the behaviour of some
I have been using WPF for a new project which is far from done.
After switching to a new mac at work, I tried to install APC as
I encountered a problem after switching from server-side JavaScript to CoffeeScript in a Node.js
After switching my Rails 2.3 app from MRI Ruby 1.8.7 to JRuby 1.6.5, the
We are looking at switching from phpundercontrol to Hudson (it looks to have some
After switching from 64-bit to 32-bit platform (both of them are CentOS) I get
I have an NServicebus project that I cannot start after upgrading from 2.6 to

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.