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.
I believe that the exception means the following:
The property values that define the referential constraints … (these are the primary key property (=
Id) value ofBidand the foreign key property (=BidId) value ofTransaction)… 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
TransactionViewModelas parameter for theEditaction,model.Transaction.BidIdandmodel.Transaction.Bid.Idare different, for example:model.Transaction.BidId.HasValueistruebutmodel.Transaction.Bidisnullmodel.Transaction.BidId.HasValueisfalsebutmodel.Transaction.Bidis notnullmodel.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
CurrentStageandEvaluation.Possible solutions:
Updatemethod of your repository (=hack)TransactionViewModel.Transaction.BidIdandTransactionViewModel.Transaction.Bid.Idto two hidden form fields with the same value so that the model binder fills both properties.Transactionproperty (and for the navigation properties inside ofTransactionas 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 …
… does not flag the related objects (
Transaction.Bid) as modified, so it would not save any changes ofTransaction.Bid. You must set the state for the related objects toModifiedas 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.