Complex Types In Entity Framework 6 Designer

Abstract

In real-world we have little or no control over data stores and data feeds we deal with due to numerous reasons:

 

  1. Age-old systems
  2. Third-Party feeds
  3. Poorly architected systems

Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually write all the time to perform basic CRUD operations.

Microsoft ORM solution Entity Framework helps us to map entities using code first or EF designer options to bring this data into existing apps easily.
 
This article discusses entities that are currently denormalized and the way to extract these into complex types for better maintainability and reusability.
 
Problem Definition
 
Most of the time entities we are dealing with are not in 3NF and would need to enhance it to meet our application needs. Also, it is more than likely we would be reusing the same entity in multiple places across the project and solution which needs additional data elements spread across multiple code files.
 
EF designer provides us a way to group the same using visual tools as explained below. As mentioned this is database first approach is used here and not code first.
 
Solution
 
I am using Visual Studio 2015 community edition for this demo, but concepts are very much applicable to Visual Studio 2013 too.
 
Step 1: Open Visual Studio 2015 or the version you currently have.
 
Select File, then New Project
 
console application
 
Figure 1
 
For purpose of this article, I have selected a console app. Enter “EmployeeComplexType” for the name.

 

Right-click Project and select Add, then New Item.
 
add new item
 
Figure 2
 
Please enter Name EmployeeDataModel.
 
EmployeeDataModel
 
Figure 3
 
Pick the first option EF designer from the database.
 
EF designer from Database
 
Figure 4
 
select your data connection
 
Figure 5
 
Connection String EFEmployeeEntities will be stored in App.ConFigure file which is primarily used to connect to SQL Server backend that I have on my local machine.
 
connect to Sql Server
 
Figure 6
 
I have created a table called Employee and included all the columns up front. Schema details are available in the following figure.
 
select table
 
Figure 7
 
Here are schema details for Employee Table selected above.
 
table design
 
Figure 8
 
refactor
 
Figure 9
 
Create New Complex Type Name by selecting name related scalar fields FirstName, Lastname, MiddleName & Salutation as shown above.
 
Click on “complexProperty” and change the name to “EmployeeName
 
EmployeeName
 
Figure 10
 
Repeat the same process for address related fields too. After changing, the Employee entity will look as per the following Figure 1.
 
Here is the final result after making related changes. Please note that in case you don’t see the Mapping Details window please press ctrl+2 or goto View, other windows, then Entity Data Model Mapping Details.
 
Entity Data Model Mapping Details
 
Figure 11
 
Here is a sample code that I have in my console app. Please note that the scalar properties like MiddleName and salutation are omitted for the second employee(name2) while initializing. Same applies to addressline2 too.
 
It is not mandatory to define all scalar properties of the complex type we have defined.
  1. var name1 = new Name { FirstName = "John", MiddleName = "M", LastName = "Doe", Salutation = "Jr." };  
  2.   
  3. var name2 = new Name { FirstName = "Jane", LastName = "Doe" };  
  4.   
  5.   
  6. var address1 = new Address  
  7. {  
  8.    AddressLine1 = "123 Elm street",  
  9.    AddressLine2 = "",  
  10.    City = "SanMateo",  
  11.    State = "CA",  
  12.    ZipCode = "94403"  
  13. };  
At the same time, we can’t have the whole complex object be set to null too.
 
The following code throws an error saying “Null value for a nonnullable value EmployeeAddress” during runtime.
  1. context.Employees.Add(new Employee { EmployeeName = name1, EmployeeAddress = address1 });  
  2. context.Employees.Add(new Employee { EmployeeName = name2, EmployeeAddress = null });  
Here is the final output.
 
final output
 
Figure 12
 
I hope this article helped you in understanding the EF designer tool's capability in defining and using complex types. Thanks, untill the next one.