EF: update root object when add/update a child

EF: update root object when add/update a child

Old forum URL: forums.lhotka.net/forums/t/8999.aspx


roberto posted on Thursday, May 27, 2010

Hi, I was looking SQL Server Profiler's trace output, and I noticed that,when I add (update) a child object, I see INSERT (UPDATE) sp call (of course!),
but I see also UPDATE sp call of root object (not dirty!).
I took an old Rolodex solution as guideline while developing my app; I'll have a deeper look at latest version of RolodexUsingPrism solution, in order to understand if it's my code, o something else..

Did anyone noticed same behavoiur ?

Thank you very much.

ps: sorry for my poor english.hope it's clear enough.

roberto replied on Thursday, May 27, 2010

I understood where the bug was.
Tomorrow I'll tell more about it. ;)

roberto replied on Friday, May 28, 2010

Well, here I am.
II coded my classes (root & child) following Rolodex-EF samples.
In RolodexUsingPrism solution, \Rolodex.Server\BusinessClasses\Company.cs, method DataPortal_Update, newCompany is created ... and CompanyName setted with CSLA's company property (newCompany.CompanyName = ReadProperty(CompanyNameProperty);).

In my root object, I query EF in order to get newCompany instead of create a new one, then set all properties with CSLA object properties.
This last step, caused EF invoke UPDATE sp.
I didn't change EF company get instance (via query), and encapsulated properties setting in "if (CSLA_object.IsSelfDirty) { // ... set all properties... }".
This prevent EF UPDATE sp call.

That's all. :)

ps: I haven't test it yet on RolodexUsingPrism solution.

RockfordLhotka replied on Friday, May 28, 2010

Do you have Julie Lerman's Programming Entity Framework book? I find this to be an invaluable resource for figuring out EF issues. Julie has really specialized in EF, and probably knows more about it than anyone outside the EF product team at Microsoft.

roberto replied on Friday, May 28, 2010

I'll get it asap.
Thank you very much.

Copyright (c) Marimer LLC