Question

I am working with Entity Framework 4 (using self-tracking entities), and accessing a view, that is the merge of two tables. So when I update the information of the view, I send the STE of the view to the repository which access to the database.

I have done the following:

When I receive my view with the updated info, I create my STE1 and STE2. How when I create an STE, this is created with an added state. So how I know for the state of the STE of the view that the state is modified, I change the state of my two STEs with the method MarkedAsModified.

Then, how I have in the STE of the view the information of the two tables, I pass the information from the view to the correct STE, and apply the changes from the STE to the objectContext.

Finally I make the saveChanges. But in this step I receive an Optimistic concurrency exception. I think that it is because The STE pass from the state to Added to Modified so the context detects that there is some modification between the creation and the SaveChanges, but also I try to AcceptChanges in the STE, later marked as modified and, apply changes and finally SaveChanges, but the problem persist.

How can I solve the problem? there is a better way to work with view and Entity Framework v4?

Thanks. Daimroc.

EDIT 1: I still have problems. My code is the following:

Components myComponent = new Components(); //this is a STE myComponent.Rereference = myView.Reference; ... //other properties myComponent.MarkedAsModified(); //this is needed because I want to update information, no add a new register. myContext.ApplyChanges("Components", myComponent); miContexto.SaveChanges();

In the saveChanges, I get the exception: update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

Which is the problem? can I not modified a new STE created?

Thanks.

Was it helpful?

Solution

I have found the way to solve the problem.

In a first solution, the solution was to make a query to the data base to get the existing registers in the database. This add the entities in the context and then it is possible to modified the data, and save the changes correctly.

But later, I found the way to avoid the needed to make the query the the data base to add the modified entities in the context.

The way is using the following code:

Customers myCustomer = new Customers  { IDCustomer = myCustomer.IDCustomer };
myContext.Customers.Attach(myCustomer);

//update the data of the entity.

myContext.SaveChanges();

The solution is to create a new entity setting the primary key. If the entity would have FK it would be indicated in the same way. At this point, the entity has the added state.

Later, is attached to the context, and then can be modified. When a field is changed, the entity changes its state to modified, so when the saveChanges() is called, EF update the entity, not try to add a new one.

I find this information in this link. In this post the solution is for deleting an entity without to retrieve it to the database, bur the idea works too if we want to modify an existing register.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top