Monday 15 September 2014

How to Create a Entity Framework model and Use it in a separate .Net Project

This article elaborate the steps required to create an Entity Data Model and re-use it in a separate .Net Project like a Console Application, a Windows Forms project, ASP.NET Project or a WPF Project.

Consider a situation where you have created a entity data model using an existing database and you want that to use in another projects. Then a question arises is this possible ,If yes then how i can achieve this? Below are the steps which will guide you to create a Entity data model and use it in another .Net project. 

Here is my previous link which will describes you how to create a entity data model from an existing database.Once it is created then the below steps will describe you how to use this model in a Console Application.

Using the Entity Model in a Console Application 
 
We will now use the model we created above, in a Console Application.
Step 1: RightClick on solution > Add > New Project > In the templates, select Windows > Console Application. Make sure the target framework is '.NET Framework 4.5'. Give it a name. We are using 'ConsoleAppUsingEFModel'. Click Ok.  
 
Step 2: Let us now add a reference to the model we created earlier, in this console application. Right click on the project > Add Reference > Go to solution folder > Projects > select the EntityFrameworkModel.dll as shown below. Click OK.


Step 3: Add a reference to the System.Data.Entity. Right click on the project > Add Reference > Go to Assemblied folder > Framework > select the System.data.entity.dll as shown below. Click OK.

   
Step 4: Then we need to provide the connection string information to the new console application. You can achieve this in two ways:
 
(a)By adding the connectionstring entry of 'EntityFrameworkModel' into the app.config file of 'consoleAppUsingEFModel' project.  
(b)By copying the App.config file of EntityFrameworkModel project in 'ConsoleAppUsingEFModel' project.(Right click the project > Add Existing Item > Go to the EntityFrameworkModel project and add the App.config file.)
 
Step 5: In this final step you need to add the Entityframework dll from the NuGet Packages. Right Click on Solution explore > Manage Nuget Packages for Solution > Go to Online > NuGet offical package source > In the search box search for EntityFramework > Click on install. Select Project Popup will open. Select the two projects > Click on Ok > License Acceptance popup will open > Click on I Accept > Click on close to close the NugetPackage.


 
Step 6: Build the solution and make sure that it is erorr free.Once the steps shown above are completed, you now need to add data in database (LogInDetail Table) using the below codes. Open Program.cs and add two name spaces :
using System.Data.Entity;
using EntityFrameworkModel;

Write the following query in your Main method.
static void Main(string[] args)
{
        using (var context = new EFDatabaseFirstEntities())
        {
              try
             {
                     var loginDetails = new LogInDeatil
                     {
                            UserName = "pallavipraharaj",
                            Password = "password",
                            DateCreated = DateTime.Now
                     };
                     context.LogInDeatils.Add(loginDetails);
                     context.SaveChanges();
                     Console.WriteLine("EFModel is successfully used in another .net project.");
                     Console.ReadKey();
             }
             catch (Exception ex)
             {
                   throw ex;
              }
        }
}

That’s all! You have now created a model and used that one in another .NET project. 
Here is the demo.Have a look.
 

2 comments: