Hello Nuno,
Thank you for your patience and cooperation.
Based on the provided information I used the
ASP.NET MVC 4 using a Domain Model example from
Telerik Data Access Samples Kit to test the scenario you need to implement. I focused on two main cases:
- When dbManutencao exists in the database.
- When dbManutencao does not exist in the database.
In the first case, the attempt to replace the values in the
dbManutencao object with those from the
manutencao object produces an error, which states that Telerik Data Access does not change the value of the primary key (
Telerik.OpenAccess.Exceptions.UnsupportedException: Unsupported Operation: Change of identity is not supported.). This error is the expected one in such scenarios. The overload of the Map() method here is:
In the same case, if I use execute the next statement, the values are copied to the
dbManutencao object, it is no longer tracked by the context, and no changes are persisted to the database:
Here, I observe the behavior you describe in point 2, and if I attach the object to the context, I get the error you experience.
In the second case, when
dbManutencao is
null (there is no object with this ID in the database), the attempt to copy the values from
manutencao was successful. On the call to the
SaveChanges() method the
manutencao object was persisted to the database and
dbManutencao was its exact copy, which was not tracked by the context. In this case, the overload of the Map() method I used was:
In the same case, if I use the overload of the Map() method shown below, the values from
manutencao are not transferred to
dbManutencao and the latter remains null.
Based on this outcome from the tests, I would suggest to you to separate the logic that adds new objects to the database from the logic that updates an existing object.
I hope you find this feasible. I am looking forward to solving the issue with you.
Regards,
Doroteya
Telerik
OpenAccess ORM is now
Telerik Data Access. For more information on the new names, please, check out the
Telerik Product Map.