Justin Helgerson
Justin Helgerson

Reputation: 25521

Updating object with related entities from detached state

When I query from the entity framework I always query in a detached state so that the records retrieved can be stored in cache for subsequent requests.

Right now I have a form that the user can edit which contains a parent record, and then two lists of parent records.

When the data is POSTed to the server, I take my view models and map them into the entity framework objects using AutoMapper. The data looks fine; AutoMapper is mapping the data correctly.

When I attach the object so that I can update it, an exception is thrown: 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.

public static void UpdateOrder(ShippingOrder shippingOrder) {
    using (OrderEntity orderContext = new OrderEntity()) {
        //Exception happens here
        orderContext.ShippingOrders.Attach(shippingOrder);
        //Update the order itself; mark the order has being modified so the EF will update it.
        orderContext.ObjectStateManager.ChangeObjectState(shippingOrder, System.Data.EntityState.Modified);
        //Perform the update.
        orderContext.SaveChanges();
    }
}

The EntityFramework (EF) seems to think that my keys aren't lining up, but I'm not sure what isn't correct. The foreign key property does have the correct value, so I'm not sure what it's checking. Does anyone have any ideas?

Upvotes: 0

Views: 1608

Answers (3)

DevDude
DevDude

Reputation: 31

You might try something like this:

ShippingOrder existingShippingOrder = orderContext.ShippingOrders.Find(shippingOrder.ID);
orderContext.Entry(existingShippingOrder ).CurrentValues.SetValues(shippingOrder);

Upvotes: 3

John x
John x

Reputation: 4031

as explained here

Insert or update pattern A common pattern for some applications is to either Add an entity as new (resulting in a database insert) or Attach an entity as existing and mark it as modified (resulting in a database update) depending on the value of the primary key. For example, when using database generated integer primary keys it is common to treat an entity with a zero key as new and an entity with a non-zero key as existing. This pattern can be achieved by setting the entity state based on a check of the primary key value. For example:

public void InsertOrUpdate(DbContext context, Unicorn unicorn)
{
    context.Entry(unicorn).State = unicorn.Id == 0 ?
                                   EntityState.Added :
                                   EntityState.Modified;
    context.SaveChanges();
}

you can try

public static void UpdateOrder(ShippingOrder shippingOrder) {
    using (OrderEntity orderContext = new OrderEntity()) {       
        orderContext.Entry(shippingOrder).State = shippingOrder.Id==0?
                                                  EntityState.Added :
                                                  EntityState.Modified;

        orderContext.SaveChanges();
    }
}

UPDATE:

for ObjectContext class you can try

 public static void UpdateOrder(ShippingOrder shippingOrder) {
        using (OrderEntity orderContext = new OrderEntity()) {                  
            orderContext.ObjectStateManager.ChangeObjectState(shippingOrder, EntityState.Modified);    
            orderContext.SaveChanges();
        }
    }

Upvotes: 0

Travis J
Travis J

Reputation: 82287

Instead of

orderContext.ObjectStateManager.ChangeObjectState(shippingOrder, System.Data.EntityState.Modified);

try this

orderContext.Entry(ShippingOrder).State = EntityState.Modified;

Upvotes: 0

Related Questions