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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:49:37+00:00 2026-05-27T08:49:37+00:00

I have searched hi and low and I am stuck here. I am using

  • 0

I have searched hi and low and I am stuck here.

I am using EF 4.1 in an MVC3 app, with the Service/Repository/UnitOfWork pattern and AutoMapper to map my models and entities.

So I have a really basic situation; I have a collection of ChildProducts that have a collection of PriceTiers.

My view models look like this:

AddEditChildProductModel

public class AddEditChildProductModel
    {
        #region "Fields/Properties"
        public ActionType ActionType { get; set; }
        public string FormAction { get; set; }

        public int ID { get; set; }
        public int ProductID { get; set; }

        public string Sku { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public decimal Cost { get; set; }
        public decimal MSRP { get; set; }
        public decimal RetailPrice { get; set; }

        public int Servings { get; set; }
        public decimal Weight { get; set; }

        public bool Display { get; set; }
        public int DisplayIndex { get; set; }

        public IEnumerable<AddEditPriceTierModel> PriceTiers { get; set; }

        #endregion

        #region "Constructor(s)"

        #endregion

        #region "Methods"

        #endregion
    }

AddEditPriceTierModel

public class AddEditPriceTierModel
    {
        #region "Fields/Properties"
        public int ID { get; set; }
        public int ChildProductID { get; set; }
        public decimal Price { get; set; }
        public int QuantityStart { get; set; }
        public int QuantityEnd { get; set; }
        public bool IsActive { get; set; }
        #endregion

        #region "Constructor(s)"

        #endregion

        #region "Methods"

        #endregion
    }

In the controller action, I am simply trying to map the changed PriceTier properties:

public ActionResult EditChildProduct(AddEditChildProductModel model)
        {
            if (!ModelState.IsValid)
                return PartialView("AddEditChildProduct", model);

            ChildProduct childProduct = productService.GetChildProductByID(model.ID);
            AutoMapper.Mapper.Map<AddEditChildProductModel, ChildProduct>(model, childProduct);
            UnitOfWork.Commit();

            return ListChildProducts(model.ProductID);
        }

And I am getting this error:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

When stepping into the action, the models/entities are mapped correctly, I don’t get it!!

  • 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-27T08:49:37+00:00Added an answer on May 27, 2026 at 8:49 am

    Eranga is right. I’m guessing your productService does not call AsNoTracking on the ef context before returning the ChildProduct. If not, this means what it returns is still attached to the context. When automapper does its thing, it replaces the whole collection, which can orphan the attached child entities that were not part of the form submission. Since the orphans don’t have a non-null foreign key, they must be deleted from the context before calling SaveChanges. If they are not, you get this infamous exception.

    On the other hand, if your productService calls AsNoTracking on the context before returning the entity, it will not track changes, and will not try to delete any orphaned items that do not exist in the collection created by automapper.

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

Sidebar

Related Questions

I have searched high and low looking for a simple class or set of
I have searched high and low for documentation on how to use this feature.
I have searched high and low for an example of editing a parents children
I have searched high and low for an answer to why query results returned
I have searched high and low and cannot find a Samsung Omnia SDK. I
I have searched hi and low for a solution for this, basicly I would
I have searched high and low, but I can't figure this one out. I
Ive searched high and low on this one. I don't have PowerCommands installed as
I have searched high and low to no avail, and this is last step
Have searched low and wide, and can't yet seem to locate a connection string

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.