Click here to Skip to main content
13,005,581 members (61,888 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 5 Mar 2014

DTO from Entity Framework Class

, 11 Mar 2014
Rate this:
Please Sign up or sign in to vote.
To inherit an Entity Framework class, to extend it, bind to data and unmap it from DBContext.


Suppose you work with an ORM (e.g.: Entity Framework) and you usually use DTO (Data Transfer Object) pattern to transfer data.

When creating a DTO class from a binding table, you need most of mapped properties, so that you must define them again in your new class. Also when you have to transfer object to a view, you need to bind it with data base values.

If you want to avoid the tedious task of fill object, you can use reflection or an IOC container as AutoMapper, but you still need to declare all properties to object.

Using inheritance you won’t need it, but, by default the class will be linked by Entity Framework to data context; also since it’s a subclass it’s not possible to cast it from its parent class.

The solution is as follows:

  1. To declare an inherited class.
  2. To unmap class from Entity Framework by using NotMapped keyword.
  3. To bind properties through reflection with its parent class.


If you are going to use this code, it’s supposed you are programming with C# and Entity Framework, as well as you have knowledge about DTO pattern and reflection.

Using the Code

Your DTO Class:

public class YourDTO: YourDBContextClass
    //Feel free to add properties. EF Won’t try to bind it with database
    Public property string FullName { get{ return (Name + " " + Surname); } }

The Controller:

public ActionResult Print(int id)
   DTO.YourDTO dtoObj = new DTO.YourDTO();
   dtoObj.CopyObject(dbContext.ParentClass.First(l =>;
   return (dtoObj);

The Reflection Method

CopyObject as an Extension Method

public static class ObjectExtensions
   public static void CopyObject(this object objTo, object objFrom)
       Type tObjFrom = objFrom.GetType();
       Type tObjTo = objTo.GetType();

       var listPropObj1 = tObjFrom.GetProperties().Where(p => p.GetValue(objFrom) != null).ToList();

        foreach (var item in listPropObj1){
            if (tObjTo.GetProperty(item.Name) != null){
                tObjTo.GetProperty(item.Name).SetValue(objTo, item.GetValue(objFrom));


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
JV999912-Mar-14 1:53
professionalJV999912-Mar-14 1:53 
GeneralRe: My vote of 1 Pin
meula12-Mar-14 2:34
membermeula12-Mar-14 2:34 
GeneralRe: My vote of 1 Pin
JV999912-Mar-14 3:38
professionalJV999912-Mar-14 3:38 
GeneralRe: My vote of 1 Pin
meula12-Mar-14 4:06
membermeula12-Mar-14 4:06 
SuggestionReflection, too slow. Pin
Ralph Varjabedian12-Mar-14 1:49
memberRalph Varjabedian12-Mar-14 1:49 
GeneralRe: Reflection, too slow. Pin
meula12-Mar-14 2:36
membermeula12-Mar-14 2:36 
GeneralRe: Reflection, too slow. Pin
Ralph Varjabedian18-Mar-14 5:45
memberRalph Varjabedian18-Mar-14 5:45 
GeneralMy vote of 5 Pin
Akhil_Mittal11-Mar-14 21:24
mvpAkhil_Mittal11-Mar-14 21:24 
GeneralRe: My vote of 5 Pin
meula11-Mar-14 23:23
membermeula11-Mar-14 23:23 
QuestionI was looking for solution like this. Pin
dzimiq9-Mar-14 22:26
memberdzimiq9-Mar-14 22:26 
AnswerRe: I was looking for solution like this. Pin
meula9-Mar-14 23:31
membermeula9-Mar-14 23:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170627.1 | Last Updated 11 Mar 2014
Article Copyright 2014 by meula
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid