Reuse The Model Classes Of Entity Data Model (.edmx) To Multiple Projects Using Class Library In ASP.NET Web API - Part Three

Introduction
 
A class library is useful when you want to share code between multiple programs. Class Library is a  collection of reusable types including classes, interfaces and data types included in the .NET Framework to provide access to system functionality. It can be used for developing applications such as console applications, Windows GUI applications, ASP.NET applications, Windows and Web services, workflow-enabled applications, service-oriented applications using Windows Communication, XML Web services, etc.
 
Real-time Example
  • The class is like a TV in a room and only room members can watch it.
  • Class lib is like a theater  in a common place (city, mall) and  anyone can watch it.

We can create the class files in all .NET applications (C#.Net,Asp.net,Asp.net MVC). In .NET, we call it  DLL (Dynamic Link Library). If you are creating the classes in your application, you can use this class only inside this application, you cannot use it in other applications.If you create the class library you can use it in all the applications.

When do you have to create and use Class Lib?

When we are writing the same code in different applications, at the time we can create the class library and use it in all the applications. 

Class Lib  In .NET and Java
  • In Java, it is called a Package.
  • In .NET, we call it DLL (Dynamic Link Library) 
Before going through this session visit my previous sessions as mentioned below. Refer to the links..
Description
 
In this session, I will show you how to use class library to implement Entity Data Model (.EDMX) classes and use it in multiple projects as I have done previously like in SatyaConsumingApi and  SatyaWebApi. The .EDMX file was created in SatyaWebApi project earlier so it can be used only for SatyaWebApi project and to get access to this feature in other projects it will be very tedious. For that reason Class Library is
a best choice to make faster.
 
Steps to be followed.
 
Step 1
 
Create class library project called Entities for separating the model classes to another project.
 
Right Click on Solution from Solution Explorer > Add > New Project > Class Library > Enter Name (here "Entities") > Add. Here I am going to add an another (class library) project for separate models classes to another project from our previously created SatyaWebApi project. So, we can reuse the model classes to multiple projects. 
 
During the creation, Class1.cs will be auto generated inside the class library (Entities) . So, you should delete that.
 
 
Step 2
 
Add EF DbContext Generator File.
 
Go to Entities > Add > Add New Item > Select "EF 5.x DbContext Generator" under Data tab > Enter Name "Satyadatabasemodel.tt" > OK.
 
 
 
Open Satyadatabasemodel.tt file and change value of const string inputFile = @"../SatyaWebApi/Satyadatabasemodel.edmx"; It is a path of .EDMX file as created earlier in  SatyaWebApi.
 
 
 
Right click on the Satyadatabasemodel.tt file and Run Custom Tool. Here "SatyaWebApi" is the project name and "Satyadatabasemodel.edmx" is the model name. 
 
 
 
Custom Tool in visual studio >>
 
A custom tool is a file generator because its purpose is to generate files from an existing file. The original intent of the tool was to generate just one file, but by writing some custom code, you can generate several. It’s a file generator and makes code-behind files. It’s stored in a ComVisible DLL file and uses the Registry.
 
You can get two autogenerated files under Entities Class Library project.
 
 >> Satyadatabasemodel.tt 
  • Employee.cs
  • Satyadatabasemodel.cs

Autogenerate code of entity data model class "Employee.cs" with related database object properties. Here Employee is a partial class that can be used in multiple projects.

  1. namespace Entities  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.       
  6.     public partial class Employee  
  7.     {  
  8.         public int EmployeeID { getset; }  
  9.         public string FirstName { getset; }  
  10.         public string LastName { getset; }  
  11.         public string EmailID { getset; }  
  12.         public string City { getset; }  
  13.         public string Country { getset; }  
  14.     }  
  15. }  
Step 3
 
Delete Satyadatabasemodel.tt file exist under Satyadatabasemodel.edmx of SatyaWebApi projects. 
 
Expand Satyadatabasemodel.edmx of SatyaWebApi projects >> Right Click On Satyadatabasemodel.tt >> Delete It.
 
 
Step 4
 
Change Custom Tool Namespace value of Satyadatabasemodel.Context.tt under Satyadatabasemodel.edmx .
 
Go to solution Explorer > select Satyadatabasemodel.Context.tt under Satyadatabasemodel.edmx > Right click and Go to Properties > Enter "Custom Tool Namespace" value. Our class library project name is "Entities" > Save. 
 
 
Step 5
 
Add Reference of newly created project (Entities) into our SatyaWebApi Application. 
 
Right Click on References of SatyaWebApi > Add Reference... > Select Projects (here "Entities") under Solution > OK. 
 
 
Step 6
 
Add Reference of newly created project (Entities) into our Web Application or Client Application named "SatyaConsumingApi".
 
Right Click on References of SatyaConsumingApi > Add Reference... > Select Projects (here "Entities") under Solution > OK.
 
 

So, when you are going to use any datacontext as in our project "CrystalGranite2016Entities" or Entity Data Model class file as "Employee" in any project then you should use the namespace with same name as Class Library name as mentioned below.
  1. using Entities; 
Here  Entities is nothing but the Class Library name, otherwise you will get an error: "Are you missing a using directive or assembly reference?".
 
SUMMARY
  • Intro to Class Library.
  • Benefits of using Class Library.
  • Add .EDMX file reference in Class Library.
  • Implement Class Library as namespace in multiple projects.


Similar Articles