1

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?

Victor Rodrigues
  • 11,353
  • 23
  • 75
  • 107

2 Answers2

1

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

Ariel Popovsky
  • 4,787
  • 2
  • 28
  • 30
  • 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 Dec 02 '09 at 15:01
1

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
  • 125,891
  • 12
  • 252
  • 273
  • thanks Craig! If the reference isn't only one, but a collection, how can I do that? – Victor Rodrigues Dec 02 '09 at 15:28
  • 1
    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 Dec 02 '09 at 15:59
  • 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 Dec 02 '09 at 18:20
  • correction: their tips works if you are adding a new entity. if you're updating an existing one, EF fails. – Victor Rodrigues Dec 02 '09 at 18:22
  • You make the stub, attach it, and add it ot the collection property. – Craig Stuntz Dec 02 '09 at 18:31
  • 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 Dec 02 '09 at 20:33
  • Even when working with single entities I'm experiencing the same. I'm convinced that using EF1 was a mistake =/ – Victor Rodrigues Dec 02 '09 at 20:36
  • 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 Dec 02 '09 at 20:41
  • 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 Dec 02 '09 at 21:02
  • You can trace SQL with SQL Profiler and don't need to modify the source if you do it that way. – Craig Stuntz Dec 02 '09 at 21:30
  • Could you be persuaded to demonstrate the 'better way to do this' in EF 4.0? – Ciel Feb 15 '10 at 15:17
  • 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 Feb 15 '10 at 16:27