1

I'm new to Entity Framework and the database first approach. Can anyone please help me?

Here is the case:

I have a clean, ordinary domain class (Person) with only properties. This class is defined in a VS-project that will only contain domain classes, without any reference to Entity Framework or other things that belong in a data access layer.

I also have a database table (tblPerson). I have created an EDMX for it and used DbContext Generator to create POCO-classes for it.

It is important to keep entity framework references separate from the project with the domain class, and I want to use a repository pattern combined with dependency injection.

The question is:

How do I "map" the Entity Framework POCO-class to my existing domain class? They have the same properties. I have read something about proxies, buddy classes and more, but didn't find any good examples.

Please help.


Lets say that the domain model class looks like this (just an example):

public class Person
{
   public int Id { get; set; }
   public string Name { get; set; }
}

and the database table has the same colums:

Id (int, not null, primary key) Name (nvarchar(50), not null)


Update:

Ok, I found a solution. I did what Ladislav Mrnka suggested and derived from ObjectContext. Here is a page that describes how it's done: Entity Framework 4.0 – Part4: How to use your own POCO’s

Jo Inge Arnes
  • 11
  • 1
  • 3
  • You don't need to have any EF POCO class unless your domain class needs something special. You can map directly to your domain class (btw. domain class with only properties doesn't sound like domain class at all). Update your question with some examples of table, poco class and domain class and describe the reason why you need additional layer of mapping - hint: keeping references to EF separate from domain classes project is not sufficient reason because your EF poco classes can be in separate project without any reference to EF. – Ladislav Mrnka Aug 19 '12 at 20:17
  • Thank you for answering. How can I map EF directly to my domain model class? That would be a solution for my question. I can't use code first, because the database already exists and is in use. – Jo Inge Arnes Aug 19 '12 at 21:32
  • Your entity in EDMX must exactly match your domain class. You need to turn off code generation and create your own derived context (if you want it) using `ObjectSet` with your domain classes. – Ladislav Mrnka Aug 19 '12 at 21:39
  • Ok. The EDMX exactly matches my domain model class, so I'll try too google and read about how I can create a derived context. Thank you very much. – Jo Inge Arnes Aug 19 '12 at 21:42
  • You can absolutley use code first for existing and new databases – devdigital Aug 19 '12 at 22:16

2 Answers2

0

If you want to map your "domain objects" to the EF generated POCO classes, then you can use a mapper such as AutoMapper https://github.com/AutoMapper/AutoMapper/wiki

podiluska
  • 50,950
  • 7
  • 98
  • 104
  • But it's important to keep the EF-functionality, like lazy loading and so on, so I want the EF-classes to extend my domain classes. Like the way it's done with code first. Just mapping the values will not suffice. What I want to do, is just to use my own classes as POCO-classes (like in code first) instead of the classes generated by DbContext Generator. – Jo Inge Arnes Aug 20 '12 at 10:56
  • Then make your domain classes a partial class of the EF class? – podiluska Aug 20 '12 at 10:59
  • podiluska: Ok, thank you. That will probably be a good way to achieve what I'm looking for. I'll look at your suggestion and also the one from Ladislav Mrnka, and update my post when I have found a solution. :) – Jo Inge Arnes Aug 20 '12 at 11:06
0

Ok, I found a solution. I did what Ladislav Mrnka suggested and derived from ObjectContext. Here is a page that describes how it's done: Entity Framework 4.0 – Part4: How to use your own POCO’s

Jo Inge Arnes
  • 11
  • 1
  • 3