This post will show you a simple way to generate DTO's (Data transfer objects) from your Entity Framework diagram by reading the XML file that is generated (.edmx) and creating DTO classes based on your diagram.
Data transfer objects are used to pass data to consumers of your web/wcf services, to prevent your data layer objects from being exposed to a third party.
The Entity Framework takes out almost all the work of creating an effective data layer in a .Net application. It's easy to use and great for most applications, but the generated objects should be used internally to access your data store, and should never be sent out to a third party.
On the left is a sample entity diagram showing a customer entity with related addresses. This sample has few entities with few properties, and you could easily create DTO objects in code which include only the properties you'd like to expose, but if you have to do this for a large object model then you can spend hours on this task. Worse still, every time you modify your model, you need to modify your DTO classes.
To automate this task we can use the XML file which is generated by the Entity Diagram designer - the .edmx file. This file contains all the mapping information which maps your database objects to your generated entities, include database types and CLR types. We simply need to read the CLR entity descriptions in this file, and generate the DTO code containing the same classes and properties. From there you can edit the DTO classes to exclude certain entities and properties.
The XML section we're interested in is shown here:

The window used to generate your DTO from allows you to select an edmx file, provide a namespace and generate the code.
No fancy reflection being used here, I simply build up the code in a string and output it in the large text box to copy and paste to your project.
I'm generating VB code here, but the application can of course easily be modified to generate C# code.
The generated code looks as follows:
Namespace DTO
Public Class Addresses
Private _ID As Int32
Private _Addres1 As String
Private _Addres2 As String
Private _Addres3 As String
Private _Addres4 As String
Private _StreetNumber As String
Private _PostCode As String
Private _City As String
Private _CountryID As Nullable(Of Int32)
Public Property ID() As Int32
Get
Return _ID
End Get
Set(ByVal value As Int32)
_ID = value
End Set
End Property
Public Property Addres1() As String
Get
Return _Addres1
End Get
Set(ByVal value As String)
_Addres1 = value
End Set
End Property
Public Property Addres2() As String
Get
Return _Addres2
End Get
Set(ByVal value As String)
_Addres2 = value
End Set
End Property
Public Property Addres3() As String
Get
Return _Addres3
End Get
Set(ByVal value As String)
_Addres3 = value
End Set
End Property
Public Property Addres4() As String
Get
Return _Addres4
End Get
Set(ByVal value As String)
_Addres4 = value
End Set
End Property
Public Property StreetNumber() As String
Get
Return _StreetNumber
End Get
Set(ByVal value As String)
_StreetNumber = value
End Set
End Property
Public Property PostCode() As String
Get
Return _PostCode
End Get
Set(ByVal value As String)
_PostCode = value
End Set
End Property
Public Property City() As String
Get
Return _City
End Get
Set(ByVal value As String)
_City = value
End Set
End Property
Public Property CountryID() As Nullable(Of Int32)
Get
Return _CountryID
End Get
Set(ByVal value As Nullable(Of Int32))
_CountryID = value
End Set
End Property
End Class
Public Class Customers
Private _ID As Int32
Private _Name As String
Private _VAT_number As String
Private _Rate As Nullable(Of Decimal)
Public Property ID() As Int32
Get
Return _ID
End Get
Set(ByVal value As Int32)
_ID = value
End Set
End Property
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property VAT_number() As String
Get
Return _VAT_number
End Get
Set(ByVal value As String)
_VAT_number = value
End Set
End Property
Public Property Rate() As Nullable(Of Decimal)
Get
Return _Rate
End Get
Set(ByVal value As Nullable(Of Decimal))
_Rate = value
End Set
End Property
End Class
End Namespace
Download the source code