Editor’s note: The following post was written by Visual C# MVP Ming Man Chan
Creating Unit Test for the projects using Microsoft’s Entity Framework
This article consist of three subsection:
- Create an ADO.NET Entity Data Model in a Console Application
- Create the Unit Test Project to reproduce the error.
- Fix the error with adding the Entity Framework assembly and connection string into the Unit Test Project.
When you are creating unit test method then you might have hit the following error:
Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception: System.InvalidOperationException: No connection string named 'NorthwindEntities' could be found in the application config file.
The error with NorthwindEntities is because I am using the sample database so for you it may be any xxxxxxEntities.
Create an ADO.NET Entity Data Model in a Console Application
Let us simulate the problem by using a Console Application, this can apply to other type of projects such as Web and Windows client. We will be using Visual Studio 2013 for this article. The Entity Framework that this article is using is version 6.0.
- Choose File -> New -> Project…
- Select Console Application from project template.
3. Right the console project.
4. Select Add -> New Item…
5. Select Data then ADO.NET Entity Data Model.
6. Type in the Name for example, NWModel.edmx.
7. Click on Add button.
8. Click on Next > button.
9. Click on Which data connection should your application use to connect to the database? (combo box) in Entity Data Model Wizard.
10. Click on New Connection... button in Entity Data Model Wizard.
11. Type on Server name: in "Connection Properties" for example, .\SQLEXPRESS
12. Click on Open button in "Connection Properties".
13. In this sample you can click on northwind in list item.
14. Click on OK button in "Connection Properties".
15. Click on Next > button in Entity Data Model Wizard.
16. Click on "Tables (tree item)" in Entity Data Model Wizard.
17. Click on dbo (tree item) in Entity Data Model Wizard.
18. Select the Products table.
19. Click on Finish button in Entity Data Model Wizard.
The ADO.NET Entity Model is now created.
20. Click on Build menu item to build your project.
Replace the following code to the Program.cs file.
public class Program
public static void AddProduct()
NORTHWNDEntities ctx = new NORTHWNDEntities();
Product product = new Product();
product.CategoryID = 1;
product.ProductName = "toy";
static void Main(string args)
The AddProduct is hardcoded for testing proposes. In real life then you might pass the ID and ProductName as arguments.
Create the Unit Test Project to reproduce the error
- Right click in ConsoleApplication1 solution.
- Left click on Add.
3. Left click on New Project...
4. Left click on OK (button) in Add New Project.
5. Right click Reference in UnitTestProject1 project.
6. Left click on Add Reference...
7. Click on ConsoleApplication1 (dataitem) in Reference Manager under Solution -> Project.
8. click on OK (button) in Reference Manager.
Now the ConsoleApplication1 added as the reference for UnitTestProject1.
Replace the UnitTest1.cs file with the following code.
public class UnitTest1
public void TestMethod1()
Right click inside the TestMothod1 then select Run Test.
Now you will get the error.
“Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception:
System.InvalidOperationException: No connection string named 'NORTHWNDEntities' could be found in the application config file.”
Fix the error with adding the Entity Framework assembly and connection string into the Unit Test Project.
Well, we can manual create an app.config file but that is not going to be easy. The easy way is add the Entity Data Model follow the step 4 through step 20 in section Create an ADO.NET Entity Data Model in a Console Application.
You must then delete the edmx and other files that were created by the wizard except the App.config file.
You can now run the unit test again. You should see it green this time.
About the author
Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400. Additionally you can read Ming’s Channingham’s blog.
About MVP Monday
The MVP Monday Series is created by Melissa Travers. In this series we work to provide readers with a guest post from an MVP every Monday. Melissa is a Community Program Manager, formerly known as MVP Lead, for Messaging and Collaboration (Exchange, Lync, Office 365 and SharePoint) and Microsoft Dynamics in the US. She began her career at Microsoft as an Exchange Support Engineer and has been working with the technical community in some capacity for almost a decade. In her spare time she enjoys going to the gym, shopping for handbags, watching period and fantasy dramas, and spending time with her children and miniature Dachshund. Melissa lives in North Carolina and works out of the Microsoft Charlotte office.