Reputation: 4524
We're using LINQ to SQL with the repository pattern and DTOs. My question is: when saving an entity to the database, the Save() method accepts a DTO as an argument. Saving a new one seems very straight forward, I just instantiate a new entity and the populate the values from the DTO, then call SubmitChanges() on the data context.
However, what happens when editing an existing entity? What is the best way to load the original values? My instinct is to create a new entity, load the values from the DTO, then Attach() it to the datacontext, but now I've lost any of the change tracking since I didn't account for the original values.
How do you guys handle this?
Upvotes: 1
Views: 262
Reputation: 58582
Since you are using a DTO, which I am assuming is not the same as your entities, you generally retrieve the entity from the context, and then set/compare the values in the entity to the DTO.
If my assumption is wrong, generally people just attach the entity to the context, and save the changes.
Upvotes: 1