2

I just started working on a project that uses Entity Framework. Prevoiusly, the app passed the EF entities around, and they were fully populated, so each section of code was able to access properties within properties with properties...

I just created domain entities and I want to map the EF entities to these domain entities. It's basically a 1-1 mapping. How is this supposed to be done? There is a T4 template that will map the EF entities to POCOs, but it only maps one level deep. What if I want to access Person -> Car -> Engine? How am I supposed to go from the EF structure to the POCO structure and have the mapping work all the way through the hierarchy?

Bob Horn
  • 33,387
  • 34
  • 113
  • 219
  • If it is one to one mapping then I think the best is to create POCO entities for the Entity Frame too and you don't need to create separate entities for domain. – Asif Mushtaq May 15 '12 at 15:13
  • How do I create the POCO entities for EF? – Bob Horn May 15 '12 at 15:43
  • You can either use POCO http://stackoverflow.com/questions/2478081/entity-framework-4-poco-where-to-start Or Entity Frame work code first – Asif Mushtaq May 15 '12 at 15:46

2 Answers2

1

You should use something like AutoMapper or ValueInjector. If your classes follow certain conventions, then it works quite well. And if they don't, you can define custom mapping classes to do it for you.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Thank you. I'm trying to avoid learning a 3rd party tool. Based on daryal's answer, I may be able to just use POCOs. – Bob Horn May 15 '12 at 16:08
  • @BobHorn - even if you're using POCO's, it's generally a bad idea to use them in your presentation layer. Data and Domain layers, sure. But depending on your UI (MVC, WPF, Silverlight, etc..) it's better to use View Models that need to be mapped to your business layer. – Erik Funkenbusch May 15 '12 at 16:11
1

In my opinion, you do not need to create another set of domain entities. POCO entities are designed to be used in all layers (IMHO).

But if you want to map another set of entities to POCO entities, just go with Automapper. It has the ability to map objects and their properties.

On the otherhand, using POCO it is possible to access Person -> Car -> Engine chain. Just make the "include"s properly, and there will be no problem.

daryal
  • 14,643
  • 4
  • 38
  • 54
  • I must be using the wrong terminology. To me, POCOs == domain entities. I don't want to map another set of POCOs to domain entities, because they're the same thing. I simply didn't want to have to make the data class (that uses EF) public. So, I think I just need two types of classes: what EF generates, and what I can pass around to all projects. Yes? – Bob Horn May 15 '12 at 15:41
  • In fact no; you just need POCO classes; you use POCO classes in EF, in your domain layer and also in service or presentation layer (you can exchange DTOs in some cases between service and client, but it is off topic). If you are using POCO classes, you do not need to generate EF classes; if I understand you properly, you are mentioning the code behind file of EDMX; you do not need these generated code at all. Just employ POCOs everywhere you need. I wish it is clear. – daryal May 15 '12 at 15:46
  • Okay, so there is really only one set of entities, and those are POCOs, right? If so, then those should live outside of my data project so they can be used by multiple projects. Now I need to see how to generate those... Maybe the link from Asif, above. – Bob Horn May 15 '12 at 15:50
  • Just create your POCO entities in a seperate library and reuse them. Here is a detailed explanation http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx – daryal May 15 '12 at 15:58
  • Thanks. The T4 template thingy already exists in this app. But the issue is that it only maps one level deep. I'm not sure how to map all the way through without hitting an infinite loop. Let me check out these links and see if they help. – Bob Horn May 15 '12 at 16:00