CSLA and Entity Framework

CSLA and Entity Framework

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


Frazer posted on Thursday, April 30, 2009

I am attempting to use EF for my data access layer.

I have an editable child list.  In the data access implementation for the child (e.g. Child_Update) I get an  EF context for my entity set, fetch the corresponding entity object for my child business object,  and update it from the properties in my child business object.  I then call SaveChanges() on the context to commit the update.

However, it seems to me that, somehow, the SaveChanges() should be getting called at the parent level, allowing me to make changes to a number of children of the parent business object, then commit all changes (or not) to the data layer with one call to SaveChanges().

Has anyone encountered this and can provide some pointers on how to approach it?

Frazer

 

RockfordLhotka replied on Thursday, April 30, 2009

You should be able to implement the save much like you do a fetch.

In other words, have the root do the EF call to re-create the EF entity model in memory. Then pass some or all of the EF model down to the child objects as you ask them to update themselves. If you are using the child data portal this should be easy, because you can just pass the appropriate entity object as a parameter to each child.

The only bit the makes update slightly harder than fetch, is that you need to map the entity object to the child, so you pass the right entity to the right child. You can easily do that with a LINQ query per child (to find the right entity), and that would be pretty inefficient. But the real question is whether that inefficiency is more or less expensive than your current approach, which is also pretty inefficient...

sergeyb replied on Thursday, April 30, 2009

If you are interested, I have a version or Rolodex that uses EF and does not round trip on updates.  You can find the code here:

 

http://cid-2383900f69b808e0.skydrive.live.com/browse.aspx/.res/2383900F69B808E0!244

 

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: Frazer [mailto:cslanet@lhotka.net]
Sent: Thursday, April 30, 2009 1:19 PM
To: Sergey Barskiy
Subject: [CSLA .NET] CSLA and Entity Framework

 

I am attempting to use EF for my data access layer.

I have an editable child list.  In the data access implementation for the child (e.g. Child_Update) I get an  EF context for my entity set, fetch the corresponding entity object for my child business object,  and update it from the properties in my child business object.  I then call SaveChanges() on the context to commit the update.

However, it seems to me that, somehow, the SaveChanges() should be getting called at the parent level, allowing me to make changes to a number of children of the parent business object, then commit all changes (or not) to the data layer with one call to SaveChanges().

Has anyone encountered this and can provide some pointers on how to approach it?

Frazer

 



Frazer replied on Thursday, April 30, 2009

Sergey,

This is a great example - I am working through it now.  I would like to restore the database in your sample - can you tell me how you created rolodex.bak  (sqlexpress, sql server version??)

thanks,

Frazer

 

Frazer replied on Friday, May 01, 2009

Sergey...

I am making progress with your example.  In the DataProtal_Insert method of the Company ER, you make this call twice:

DataPortal.UpdateChild(ReadProperty(ContactsProperty), this, newCompany);

Once before adding the newCompany entity to the entity set and then again, before saving changes.

Why twice?

Great example - this has cleared up a lot of my questions.

Frazer

 

roberto replied on Thursday, May 27, 2010

any news about that twice call?

I'm refactoring for performance, and I need to better understand those calls.
thank you

Copyright (c) Marimer LLC