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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:40:57+00:00 2026-05-27T23:40:57+00:00

I am using Entity Framework and .Net 4 MVC 3. In my create method

  • 0

I am using Entity Framework and .Net 4 MVC 3. In my create method on my controller I take in an employee object which I use to create the entry in my database.

public class Employee 
{
    public int Id { get; set; }
    public int ManagerId { get; set; }
    public virtual Employee Manager { get; set; }
}

public ActionResult Create(Employee model)
{
    if (ModelState.IsValid)
    {
        db.Employees.Add(model);
        db.SaveChanges();

        // Now I want to use the navigation properties
        Notification(model.Manager.Name);
    }
}

I post the manager Id back from the view. Now if I redirect back to the details page the manager has been created – however when I try and access it like the above it is null. What I have had to resort to is:

if (ModelState.IsValid)
{
    model.Manager = _employeeRepository.Get(model.ManagerId);
    db.Employees.Add(model);
    db.SaveChanges();

    // Now this works
    Notification(model.Manager.Name);
}

However this doesn’t seem right. Surely EF creates the Manager object for me so why do I need to manually get and set it? Am I doing something wrong?

  • 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-27T23:40:58+00:00Added an answer on May 27, 2026 at 11:40 pm

    Though it may seem wrong, that’s the intended functionality and your solution is approximately correct. The EF DbContext won’t grab the Manager property automatically because it could potentially be costly to do that. If it did it automatically and did not want that, you’d be angry with EF. The answer (and your original solution) is to explicitly get the data on a subsequent call.

    I would suggest a slightly different implementation:

    if (ModelState.IsValid)
    {
        db.Employees.Add(model);
        db.SaveChanges();
    
        // Get the manager name only after the SaveChanges is successful
        // Will fail if the manager ID is not valid
        var managerName = db.Managers.Where(mgr => mgr.ManagerId == model.ManagerId).Select(mgr => mgr.Name).Single();
        Notification(managerName);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using VS 2010 beta 2, ASP.NET MVC. I tried to create an Entity framework
Using Entity Framework, I suddenly get this strange error after publishing my asp.net mvc
I am using the asp.net mvc with the Entity Framework. I have a list
I have an ASP.NET Dynamic Data application (using Entity Framework) in which I have
I am building an ASP.NET MVC web application using entity framework DbContext using the
To begin with: Using Entity Framework v4.0. ASP.NET MVC 2.0. Visual Studio 2010. I
I'm using ASP.NET MVC 3 with Entity Framework 4. When a user logs in,
I'm building an ASP.NET MVC site using the ADO.NET Entity Framework. I have an
I am trying use MySql and Entity Framework, using Connector/Net 6.1 with this as
I'm building an ASP.NET MVC 3 site using the code-first Entity Framework 4 approach.

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.