The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

johnicello (8) [Avatar] Offline
#1
Since this is the first time posting for this book, let me say that I'm getting a lot out of it; it's helping me learn EF in a deeper way. Thanks.

As far as 'ApplyCurrentValues', you show an example on page 179 + 180, where the address info was being lost because the disconnected client wasn't explicitly setting those properties. And that the solution was to use ApplyCurrentValues:

ctx.Companies.OfType<Customer>().First(c => c.CompanyId ==cust.CompanyId);
ctx.Companies.ApplyCurrentValues(cust);
ctx.SaveChanges();

However, when I test this, the behavior I get is different. What happens is that the 'null' values are saved back in the database. ApplyCurrentValues seems to set all values from 'cust' including the null ones.
stefano.mostarda (47) [Avatar] Offline
#2
Re: ApplyCurrentValues not working as described
Hi,

First of all thanks for your feedback. It's helps a lot in shaping the book. Fell free to post other comments smilie.

Coming to your point, there's something I ave to explain better (and I will do in the book).

ApplyCurrentValues overwrites entry's current values. It then checks, property by property, if original values are different from current ones. Those that are different are marked as modified. consequently, optimized sql is issued.

If input entity contains null properties, they overwrite current values and consequently the property is marked as modified. In the end null values are persisted and you lose data.

I hope this sounds better to you.

Byez
Stefano