Create an Entity Framework Model and Use it in Multiple Projects

Posted by: Suprotim Agarwal , on 12/7/2010, in Category Entity Framework
Views: 243951
Abstract: This article shows the steps required to create an Entity Data Model which can be used in multiple projects.

In one of my previous articles, I had written on how to Create an Entity Data Model From a Database. A reader Phillip posted a question in the comments section, asking if it was possible to use the same model in multiple projects.

The answer is – To consume an Entity model in separate projects, you have to create the model in its own project, i.e. a class library project. Let us see how. The steps of creating the model is similar to the one I wrote in my previous article, so I won’t be explaining them over here again.

Create an Entity Data Model as a Separate project

Here are the steps to create a model as a separate project.

Step 1: Open Visual Studio 2010 > File > New Project > Windows Template > Class Library > Give the project a name ‘MyEntityModel’ > OK

Entity Model Class Lib

Delete the default Class1.cs that gets created in the project.

Step 2: Right click on the project in Solution Explorer > Add > New Item. Select the Data Template > ADO.NET Entity Data Model and click Add.

Add EF DataModel 

In the Entity Data Model Wizard, choose ‘Generate From Database’ and click Next

EF DataModel Wizard Choose Model 

Create a new Data Connection or choose from an existing one. Since we already created the Data Connection in my previous article, we will use an existing Connection

EF Data Connection 

The Entity Connection Settings will be stored in the App.Config file as ‘NorthwindEntities’.

Step 3: Now choose the Database Objects you want to include in your Entity Data Model (EDM). This time, I chose two tables ‘Customers’ and ‘Orders’ as well as two Stored Procedures too as shown below, and renamed the model namespace as ‘NorthwindModel’. Click Finish to complete the wizard. 

Entity DataModel Wizard Tables 

Build the project in Release mode. A model defined by the Entity Data Model is ready and can now be used in your applications.

Exploring the MetaData Artifact Processing property

Double click to open the model and check it’s MetaData Artifact Processing property in the Properties window, which is by default set to ‘Embed in Output Assembly’, as shown below. 

MetaData Artifact Processing 

This property is very useful in scenarios where you want to use the use the model in different projects. Since the model is created as a class library project (.dll), all you need to do now is make a reference to the .dll in your project and supply the connection string. The project using this model, finds the schema files (.csdl, .mls, .ssdl) through the metadata property. Remember that all the schema files are embedded into the .dll as resources. Open the App.config file and check the connection string entry, which contains the schema files entries, as shown in the screenshot below.

Entity Model AppConfig

Note: If you are new to schema files, read my article Exploring how the Entity Data Model (EDM) works.

You can now use this model in multiple projects like a Console Application, ASP.NET Project, a Windows Forms project or a WPF Project. Make sure you have build the project (Go to Build > Build MyEntityModel) and there are no errors.

Using the Entity Model in a Console Application

We will now use the model we created above, in a Console Application.

Step 1: Open Visual Studio 2010 > File > New Project > In the templates, select Windows > Console Application. Make sure the target framework is ‘.NET Framework 4.0’. I have called the application ‘ConsoleAppUsingMyModel’. Click Ok.

Step 2: Let us now add a reference to the model we created earlier, in this console application. Right click the project > Add Reference > Browse Projects Tab and browse to the MyEntityModel project. Go to the 'bin' folder > Release and select the MyEntityModel.dll as shown below. Click OK. 

Entity Model 

Also make sure you add a reference to the System.Data.Entity, this time using the .NET tab 


Step 3: In this final step, our console application needs to have the information about the connection string which contains the metadata and details of the database connection.

Note: If you skip this step and directly query the model, you will get an error The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Now you can either create your own .config file in this project and add the connectionstring entry of your model or simply copy the App.config file that you created in the MyEntityModel project, in your Console App project. We will adopt the simpler way. Right click the project > Add Existing Item > Go to the MyEntityModel project and add the App.config file.

Step 4: Once the steps shown above are completed, you now need to query against the model in order to fetch data. We will write code that displays the CustomerId and CompanyName of customers who have placed orders after the 1st of January, 1998.

Open Program.cs and write the following query in your Main method 

Query Entity Model 

You should get the following output

EF Query Results 

That’s it! You have now created a model that can be used in multiple projects. Check my entire Entity Framework 4.0 article series over here.

The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Naomi on Tuesday, December 7, 2010 7:08 PM
I'm a regular reader of your articles. Very nicely written and easy to follow steps
Comment posted by Suprotim Agarwal on Tuesday, December 14, 2010 1:21 AM
Thanks Naomi :)
Comment posted by maxisam on Tuesday, January 11, 2011 12:32 PM
A great article. Thanks for your work !
Comment posted by maxisam on Tuesday, January 11, 2011 12:34 PM
BTW, I hope you will keep writing more article about EF. I really think your work is very very helpful. Thanks !
Comment posted by Suprotim Agarwal on Sunday, January 16, 2011 2:20 AM
maxisam: Absolutely! You will see many more articles on EF
Comment posted by Andrea on Thursday, May 12, 2011 3:39 AM
Thank you very much for you articles, very useful and well formed.
Comment posted by sachinpatil10d on Monday, February 20, 2012 3:06 AM
nice article!!!
Comment posted by RAJU on Friday, March 23, 2012 8:29 PM
There are two different projects i.e., folders with in Data Objects layer [ Class Library] where both projects have same entity model (database).

It is evident that being a single assembly CLR throwing the exception below.

Previously found CLR type 'DataObjects.TestOrg.Entities.LocalState', newly found CLR type 'DataObjects.Org.Entities.LocalState'.

Googling yeilds that this is known issue for a while. Is there any fix in the framework it self ?

Please inform.

Comment posted by Asif Hussain on Tuesday, April 10, 2012 7:54 AM
Can i use this approach with asp mvc pattern
Comment posted by rakesh on Friday, May 18, 2012 12:05 AM
vaery useful confusion only clarity.
Comment posted by Chirag on Monday, August 20, 2012 5:28 AM
How to make setup for window Application with Entity model Connectivity
Comment posted by Chirag on Monday, August 20, 2012 5:38 AM
How to make setup for window Application with Entity model Connectivity
Comment posted by Mikkel Jersin on Wednesday, August 29, 2012 5:33 PM
Nice article.

Works great when I use the Entity Model in a console application. But I can't get it to work in an asp mvc4 project!?

where should the connectionstring go? I tried to add it to the web.config file under <connectionStrings> but that didn't work.
Comment posted by wwwwwww on Thursday, September 6, 2012 4:05 AM
Comment posted by Puja Verma on Tuesday, November 20, 2012 3:16 AM
Very nice article for beginers..Really Helpful
Comment posted by Wilson on Monday, December 10, 2012 10:42 AM
Thanks. God bless you!
Comment posted by ehsan yazdani rad on Tuesday, April 30, 2013 12:37 AM
Is it possible break down edmx to two separate project? for example one project Only be model and one project be context!?
Thank you.
Comment posted by lokender kumar on Friday, May 17, 2013 4:28 AM
Nice article.

Works great when I use the Entity Model in a console application. But I can't get it to work in an asp mvc4 project!?

where should the connectionstring go? I tried to add it to the web.config file under <connectionStrings> but that didn't work.
Comment posted by Paul on Thursday, June 27, 2013 7:45 AM
Can I use this approach for MVC3 project? I have 2 websites using the same Model. Can I put all the query in the Entity and create a dll then use it inside my MVC project?
Comment posted by zahra saffar on Friday, July 19, 2013 12:51 PM
Hi, I used Entity Framework for my project . but I have som problem with connection . I couldn't find any good way to solve it . please help me . how can I send you a picture of Error? I appreciate you if add me in yahoo messenger . maybe I can explain my problem better if we talk online. thanx
Comment posted by id saffar on Thursday, August 1, 2013 8:14 PM
Hi, can you send me your email?
Win 764 bit at yhoo com
Comment posted by anne on Tuesday, September 10, 2013 9:09 AM
Awesome!Worked like a charm. Thank you!
Comment posted by raj on Thursday, October 17, 2013 1:27 AM
Can you tell me how to create two app with sepaerate icon in single installation?
Comment posted by Nikhil on Wednesday, August 20, 2014 11:35 PM
Hi Suprotim,
i have the same requirement to make a seperatet data project which will resposible only for database communication through edmx.i have implemented this concept into mvc application where i am accessing the dll of class library project but getting the below error

No connection string named 'ABC' could be found in the application config file.

one more thing i have created a folder inside the class library project with name of "App_Data" where i have placed the mdf file of database and made a respective connection string from wizard.

Any help would be appriciated.
Comment posted by Suprotim Agarwal on Thursday, August 21, 2014 9:38 AM
@Nikhil: Which version of EF are you using? Did you try copying the connections string to the .config file in your ASP.NET MVC project?