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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:27:46+00:00 2026-05-28T06:27:46+00:00

I am trying to update a complex model in a single view. I am

  • 0

I am trying to update a complex model in a single view.
I am using ASP.NET MVC3, Entity Framework with Code first, unit of work, generic repository pattern..
but when I try to update the model, i come up with this error:

A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

Here is my simplified view model:

public class TransactionViewModel
{
     public Transaction Transaction { get; set; }
     public bool IsUserSubmitting { get; set; }
     public IEnumerable<SelectListItem> ContractTypes { get; set; }
}

Here is my simplified complex model, and as an example one of its navigation property.
Transaction has one to one relationship with all of its navigation properties:

public class Transaction
{
    [Key]
    public int Id { get; set; }

    public int CurrentStageId { get; set; }

    public int? BidId { get; set; }

    public int? EvaluationId { get; set; }

    public virtual Stage CurrentStage { get; set; }

    public virtual Bid Bid { get; set; }

    public virtual Evaluation Evaluation { get; set; }

}

public class Bid
{
    [Key]
    public int Id { get; set; }

    public string Type { get; set; }

    public DateTime? PublicationDate { get; set; }

    public DateTime? BidOpeningDate { get; set; }

    public DateTime? ServiceDate { get; set; }

    public string ContractBuyerComments { get; set; }

    public string BidNumber { get; set; }

    public DateTime? ReminderDate { get; set; }

    public DateTime? SubmitDate { get; set; }

}

Using the same view model, I am able to create a transaction object, which would populate the database like this.

Id: 1, CurrentStageId: 1, BidId: 1, EvaluationId: 1

but, when I try to update properties within these navigation properties, this line causes the error, in controller:

[HttpPost]
public ActionResult Edit(TransactionViewModel model)
{
    if (ModelState.IsValid)
    {
        -> unitOfWork.TransactionRepository.Update(model.Transaction);
           unitOfWork.Save();
           return RedirectToAction("List");
    }
}

In generic repository:

public virtual void Update(TEntity entityToUpdate)
{
 -> dbSet.Attach(entityToUpdate);
    context.Entry(entityToUpdate).State = EntityState.Modified;
}

The problem is further complicated because I should be able to edit any of the fields(properties) within any of the navigation property within Transaction object within a single view.

  • 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-28T06:27:47+00:00Added an answer on May 28, 2026 at 6:27 am

    I believe that the exception means the following:

    The property values that define the referential constraints … (these are the primary key property (= Id) value of Bid and the foreign key property (= BidId) value of Transaction)

    … are not consistent … (= have different values)

    … between principal … (= Bid)

    … and dependent … (= Transaction)

    … objects in the relationship.

    So, it looks like the following: When the MVC model binder creates the TransactionViewModel as parameter for the Edit action, model.Transaction.BidId and model.Transaction.Bid.Id are different, for example:

    • model.Transaction.BidId.HasValue is true but model.Transaction.Bid is null
    • model.Transaction.BidId.HasValue is false but model.Transaction.Bid is not null
    • model.Transaction.BidId.Value != model.Transaction.Bid.Id

    (The first point is probably not a problem. My guess is that you have situation 2.)

    The same applies to CurrentStage and Evaluation.

    Possible solutions:

    • Set those properties to the same values before you call the Update method of your repository (=hack)
    • Bind TransactionViewModel.Transaction.BidId and TransactionViewModel.Transaction.Bid.Id to two hidden form fields with the same value so that the model binder fills both properties.
    • Use also a ViewModel for your inner Transaction property (and for the navigation properties inside of Transaction as well) which is tailored to your view and which you can map appropriately to the entities in your controller action.

    One last point to mention is that this line …

    context.Entry(entityToUpdate).State = EntityState.Modified;
    

    … does not flag the related objects (Transaction.Bid) as modified, so it would not save any changes of Transaction.Bid. You must set the state for the related objects to Modified as well.

    Side note: If you don’t have any additional mapping with Fluent API for EF all your relationships are not one-to-one but one-to-many because you have separate FK properties. One-to-One relationships with EF require shared primary keys.

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

Sidebar

Related Questions

I'm trying to update an NText field in SQL 2000 using Classic ASP. Here
I'm using ASP.Net with MVC 3 and trying to auto generate images. In my
I trying to update a model on a callback but the validation is causing
So I'm trying to update an object in my MS SQL 2005 database using
I have a 'Complex' model which hasMany Unit. My model associations are correct, everything
Hi I am trying to draw a complex view, defined by an xml layout
I have an ASP.NET MVC 3 Web Application using Linq-to-SQL for my data access
I'm adding a very simple view (or trying to) to my entities object model.
I'm using Qt/C++ and trying to draw a large and complex QGraphicsScene. Once I
I'm using symfony 1.4.10, and trying to do a rather complex INSERT into a

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.