entity framework - How to update complex model in ASP.NET MVC 3 -



entity framework - How to update complex model in ASP.NET MVC 3 -

i trying update complex model in single view. using asp.net mvc3, entity framework code first, unit of work, generic repository pattern.. when seek update model, come error:

a referential integrity constraint violation occurred: property values define referential constraints not consistent between principal , dependent objects in relationship.

here simplified view model:

public class transactionviewmodel { public transaction transaction { get; set; } public bool isusersubmitting { get; set; } public ienumerable<selectlistitem> contracttypes { get; set; } }

here simplified complex model, , illustration 1 of navigation property. transaction has 1 1 relationship of 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 same view model, able create transaction object, populate database this.

id: 1, currentstageid: 1, bidid: 1, evaluationid: 1

but, when seek update properties within these navigation properties, line causes error, in controller:

[httppost] public actionresult edit(transactionviewmodel model) { if (modelstate.isvalid) { -> unitofwork.transactionrepository.update(model.transaction); unitofwork.save(); homecoming redirecttoaction("list"); } }

in generic repository:

public virtual void update(tentity entitytoupdate) { -> dbset.attach(entitytoupdate); context.entry(entitytoupdate).state = entitystate.modified; }

the problem farther complicated because should able edit of fields(properties) within of navigation property within transaction object within single view.

i believe exception means following:

the property values define referential constraints ... (these primary key property (= id) value of bid , foreign key property (= bidid) value of transaction)

... not consistent ... (= have different values)

... between principal ... (= bid)

... , dependent ... (= transaction)

... objects in relationship.

so, looks following: when mvc model binder creates transactionviewmodel parameter edit action, model.transaction.bidid , model.transaction.bid.id different, example:

model.transaction.bidid.hasvalue true model.transaction.bid null model.transaction.bidid.hasvalue false model.transaction.bid not null model.transaction.bidid.value != model.transaction.bid.id

(the first point not problem. guess have situation 2.)

the same applies currentstage , evaluation.

possible solutions:

set properties same values before phone call update method of repository (=hack) bind transactionviewmodel.transaction.bidid , transactionviewmodel.transaction.bid.id 2 hidden form fields same value model binder fills both properties. use viewmodel inner transaction property (and navigation properties within of transaction well) tailored view , can map appropriately entities in controller action.

one lastly point mention line ...

context.entry(entitytoupdate).state = entitystate.modified;

... not flag related objects (transaction.bid) modified, not save changes of transaction.bid. must set state related objects modified well.

side note: if don't have additional mapping fluent api ef relationships not one-to-one one-to-many because have separate fk properties. one-to-one relationships ef require shared primary keys.

asp.net-mvc entity-framework ef-code-first unit-of-work

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -