tags:

views:

622

answers:

4

Hello,

I have a question that is pretty similar to this question:

http://stackoverflow.com/questions/899734/strongly-typed-asp-net-mvc-with-entity-framework

but the solutions for that question don't work for me. Let me start by saying that I don't know a great deal about the subject I am asking about.

I have the following code

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(PaymentInformation paymentInformationToEdit, int pensionFundID)
{
var originalPaymentInformation = (from PIs in _db.PaymentInformation
                                  where PIs.employeeNumber == paymentInformation.employeeNumber
                                  select PIs).First();

var laborUnion = (from LUs in _db.LaborUnion
                  where LUs.laborUnionID = laborUnionID
                  select LUs)First();
paymentInformationToEdit.laborUnion = laborUnion;

_db.ApplyProperyChanges(originalPaymentInformation.EntityKey.EntitySetName, paymentInformationToEdit);
_db.SaveChanges();
}

I get an error when I try for the ApplyProperyChanges saying 'The existing object in the ObjectContext is in the Added state. Changes can only be applied when the existing object is in an unchanged or modified state'.

I don't know how to change the state to either, or even if I am doing something fundamentally wrong. Please advice.

EDIT: I hope this is the way to go here on stackoverflow. I haven't gotten an answer that solved my problem but Gregoire below posted a possible solution that I didn't understand. I hope this edit bumps my question so somebody will see it and help me. Sorry if this is not the way to go.

A: 

You need to update the information of originalPaymentInformation with the one of paymentInformationToEdit then to ApplyProperyChanges on originalPaymentInformation

Gregoire
I'm not sure I understand. I tried doing:originalPaymentInformation.Employee = paymentInformationToEdit.Employee; but that just gives me an error saying 'A referential integrity constraint violation occurred. A property that is a part of referential integrity constraint cannot be changed when the object has a non-temporary key'. Am I misunderstanding your answer ?
Mannsi
A: 

An entities state changes when you modify it. It must be in the detached state. If you modify a relationship, it will go into the added state. This means you can't edit the laborUnion relationship before you apply changes. However, I can't figure out how to change the relationship without the framework automatically adding a new entity, which is exactly the same save the relationship. Hope that helps you on your way.

anon
A: 

This is becouse you can't edit references in datached mode. Object should has ObjectContext to be able for that change. Solution for your problem is to update reference in oryginal object and rest - "static" - fields by the ApplyProperyChanges:

public ActionResult Edit(PaymentInformation paymentInformationToEdit, int pensionFundID)
{
var originalPaymentInformation = (from PIs in _db.PaymentInformation
                                  where PIs.employeeNumber == paymentInformation.employeeNumber
                                  select PIs).First();

var laborUnion = (from LUs in _db.LaborUnion
                  where LUs.laborUnionID = laborUnionID
                  select LUs)First();
originalPaymentInformation.laborUnion = laborUnion;

_db.ApplyProperyChanges(originalPaymentInformation.EntityKey.EntitySetName, paymentInformationToEdit);
_db.SaveChanges();
}

JJ Roman
A: 

If anyone is still facing this issue listen carefully to the contributions by 'anon' and 'JJ Roman'. I've changed my code accordingly and I got rid of the problem. It doesn't strike me as behaviour that is obvious or intuitive. But this is my first attempt at a project with the Entity Data Framework so I guess there are a few things I'll need to learn along the way. Thanks to everyone for your contributions.

Eric