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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:00:11+00:00 2026-05-19T01:00:11+00:00

I have a Create New Employee ASP.Net MVC form. My complex object is an

  • 0

I have a “Create New Employee” ASP.Net MVC form.

My complex object is an Employee and an Employee has an Address, which is another Complex object.

On my View/Form I collect all required values for both the Employee and the Address.

I’m using the bound HTML helpers such as:

Html.TextBoxFor(model => model.EmployeeAddress.StreetName)

AND

Html.TextBoxFor(model => model.NewEmployee.FirstName)

etc

This is all working beautifully well. Model binding is working like a dream, both server side and client side validation using DataAnnotations is working beautifully well and I am nicely receiving my populated complex objects as expected in the Controller..

Now I’m trying to save..

  • The Employee should always be newly created, because its an “Add New Employee” form.
  • But sometimes the Address is an existing Address and I don’t want to insert another one.
  • Rather, I just want to link the Employee to the existing AddressID of the one that already exists in the database.

So I wrote a nifty GetExistingOrCreateNewAddress(Address PostedAddress) method which works great so I end up with the correct Address to use and link to the about to be saved Employee object. This is all happening in the same DataContext, so no problems there..

BUT even when I link the about to be saved Employee object to an existing Address, on save a new/empty Address row is created in my Addresses table. Even though the newly created Employee does link correctly to the existing Address I told it to!

Why is it so??? And how can I save the new Employee without LINQ automatically creating a blank Address for me. Because I’m explicitly specifying an existing Address it should be linked to instead!

This is what my controller looks like:

    [HttpPost]
    public ActionResult CreateEmployee(EmployeeDetailsViewModel NewEmployeeDetails)
    {
        if (ModelState.IsValid)
        {
            EmployeeRepository ER = new EmployeeeRepository();

            // Fetch or Create the appropriate Address object for what has been entered
            Address ActualAddress = ER.GetExistingOrCreateNewAddress(NewEmployeeDetails.EnteredAddress);

            // Link this Address to the "about to be saved" Employee
            NewEmployeeDetails.Employee.Address = ActualAddress;

            // Lock it in..
            ER.SaveNewEmployee(NewEmployeeDetails.Employee);

I’m not getting any errors whatsoever!

Can anyone explain??

  • 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-19T01:00:11+00:00Added an answer on May 19, 2026 at 1:00 am

    I figured it out.

    Inside my Address complex object, it also has a further child complex object called “Suburb”. I was going to the database within the same DataContext and fetching the appropriate Suburb object to attach to my Address object.

    This was all being done inside a custom method inside my Data Repository class.

    Even after later nulling that same Address out and assigning its value to SomeExistingAddress, LINQ or model binding still felt that it needed to create a blank Address. This was because I had previously linked the above chosen Suburb record (attached to the database, wasn’t using ToList()) to a detached/local Address object. As a result, whether I liked it or not, LINQ felt that it needed to now create a blank new (but with Primary Key) Address object because the linked Suburb had to belong to something, which was previously detached. I hope that makes sense. So my solution was that when fetching the Suburb out to populate in the Address which in turn was to populate the Employee, I had to make sure I fetched the Suburb with ToList() before doing that query to find the right one. Without ToList() it created some expectation that I needed the parent entity freshly re-created even after nulling it out.

    Really weird and I don’t understand it fully yet, but kind of do. My lesson:

    “When needing to populate child complex object values of a detached/local parent complex object, ensure you only attached other detached objects, never attach pre-attached ones. Because LINQ will forcibly create a blank copy of the complex parent object simply because it is detached from the database while its child object isn’t, therefore it thinks it needs to Insert a new instance of the parent. Instead, just work locally/detached all the way, and SubmitChanges together at once.

    This was the old broken code:

    FauxAddress.Suburb = db.Suburbs.Where(x => x.SuburbName == FauxAddress.Suburb.SuburbName && x.Postcode == FauxAddress.Suburb.Postcode).FirstOrDefault();
    

    This is the new/resolved code:

    FauxAddress.Suburb = db.Suburbs.ToList().Where(x => x.SuburbName == FauxAddress.Suburb.SuburbName && x.Postcode == FauxAddress.Suburb.Postcode).FirstOrDefault();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For those that create ViewModels (for use by typed views) in ASP.NET MVC, do
I'm struggling with Html.Checkbox in ASP.NET MVC. Imagine an Employee with a repeating group
I have an Employee model that has a Name and Company I want to
Let's say I have a class Employee and a class Company which holds a
Using an update panel in ASP.NET and with the help of a good tutorial
We use ADP for employee information. I had to create a small app that
Say I have an Employee class and one of its properties is of type
I am preparing for the development of an enterprise-style application for a very small
One of the projects I was in charge of as a JSP/Struts developer at
We're having a problem getting Word documents to download in IE7/IE8 using window.open calls.

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.