views:

477

answers:

2

I use EF for Visual Studio 2008, so I don't have POCO integration with EF.

Since we have a n-tier application, we're mapping POCO to entities constantly, entities aren't thrown to the upper layers, I do mapping with Automapper and manually also.

The big problem we have is when mapping from POCO to entities.

If I'm adding a new entity that has a relationship with existing entities (e.g. adding a new Account for an existing Client), I have the existing entity POCO, and it's needed to get the related entities (e.g. the Client) from the database, this is just something very slow.

And there is my question:

How can I create an entity, properly attached to the context and all that, from a POCO object, without making any call to the database?

+1  A: 

Try the EF POCO Adapter sample: http://code.msdn.microsoft.com/EFPocoAdapter

Ariel Popovsky
hum, our POCOs have sometimes different properties and methods, I don't know if in this step of the project is hard to make this kind of change. I would like to know if there is a way to do this mapping poco -> valid entity manually. But thanks, I'll check it, maybe this can solve our problems without a big impact in the way things are being done now.
Victor Rodrigues
+1  A: 

Set the EntityKey instead of loading the related object from the DB (note there's a better way to do this in EF 4; this is for EF 1):

var account = new EntityAccount
              {
                  Name = pocoAccount.Name,
                  // etc.
              };
// now instead of doing account.Client = Context.Clients.Where(...
account.ClientReference.EntityKey = 
    new EntityKey("MyEntities.Clients", "Id", pocoAccount.Client.Id);

This does no DB access at all.

Craig Stuntz
thanks Craig! If the reference isn't only one, but a collection, how can I do that?
Victor Rodrigues
Victor: Use stubs. See: http://blogs.msdn.com/alexj/archive/2009/06/19/tip-26-how-to-avoid-database-queries-using-stub-entities.aspx
Craig Stuntz
great resource, but they don't actually show how to reference a collection in a stub way, their tips only works for single references.
Victor Rodrigues
correction: their tips works if you are adding a new entity. if you're updating an existing one, EF fails.
Victor Rodrigues
You make the stub, attach it, and add it ot the collection property.
Craig Stuntz
I do this, and when I do the update in the 'Account' entity, it returns foreign constraint error =/ I've seen people reporting the same error in this very post
Victor Rodrigues
Even when working with single entities I'm experiencing the same. I'm convinced that using EF1 was a mistake =/
Victor Rodrigues
It works correctly for me, so if you'd like help on that you need to give more details. You can blame the tool if you like, but it won't fix the problem. How about a SQL trace instead?
Craig Stuntz
ehehe, I'm not in that mood of blaming the tool, only a little bit frustrated with the framework. I'm putting the EFProviderWrappers for tracing the SQL, so I should have more details on what's happening. Thanks for the help, Craig.
Victor Rodrigues
You can trace SQL with SQL Profiler and don't need to modify the source if you do it that way.
Craig Stuntz
Could you be persuaded to demonstrate the 'better way to do this' in EF 4.0?
Stacey
Just use FK associations, which are the default in EF 4 anyway. Googling that phrase will turn up all you need to know.
Craig Stuntz