Tuesday, September 20, 2016

C#/Entity Framework 6: Project and Connection String Management

I seem to explain/implement the same fundamentals of working with Entity Framework again-and-again each time I start a new project. The steps discussed apply to both on-premise SQL Server and SQL Azure. This posting will demonstrated how to setup a class library containing a Entity Framework Model generated using a database and how to implement the database's connection string using an application/web configuration file from a a different assembly.

The solution and projects for this post are as follows:

The code to demonstrate the basic setup of an Entity Framework is Visual Studio 2015. The solution and projects for this demonstration can be seen above where:
  • EntityFramework6.Master; the solution containing the projects used to implement the code
  • DataAccess: class library that will ultimately contain the Entity Framework Model generated from a SQL Server on-premise instance.
  • MainApplication: console application which references the DataAccess assembly in order to access the underlying database.
    • The App.Config of the console application will hold the database connection string used by the DataAccess assembly.
An Entity Framework Model is added to the DataAccess, class library, project by right clicking on the project name in solution explorer and selecting Add | New Item from the context menu:

Select New Items displays the Add New Item dialog which is as follows:

Notice in the Add New Item dialog that the category of project selected as Installed | Visual C# Items | Data and that the project type is ADO.NET Entity Data Model. Once this is selected a name can be entered as shown below where the name is School:

Clicking on the Add button on the Add New Item dialog displays the following:

The model selected from the previous screen of the Entity Data Model Wizard is "EF Designer from database" which means the wizard will be pointed at an existing database via a connection and from which the Entity Framework will generate the model. Clicking on Next advances the Entity Data Model Wizard as follows:

A database connection can be selected from the drop down on the previous screen or can be created using the New Connection button. Once a connection is selected, click on Next:

The next screen in the Entity Data Model Wizard allows the version of Entity Framework to be selected. In the previous screen, Entity Framework 6.x is selected following which Next can be clicked on which displays:

The previous screen of the Entity Data Model Wizard allows the data objects from which the model is generated to be selected. In the previous dialog the tables only were selected. Doing this followed by clicking on Finish displays the model generated by Entity Framework:

Once the model is generated the app.config of the class library, DataAccess, will be populated with a database connection string at line 8. The app.config is shown below: 

The console application, MainApplication, already contains a reference to the Data Access, class library project. The rudimentary code to access the generated Entity Framework models is found in the MainApplication application's Program.cs file:

In the previous screenshot, Visual Studio is displaying error messages. This occurs because the NuGet package for Entity Framework 6.x are missing. The required NuGet package was added to the class library, DataAccess, when the Entity Framework Model was added.

To manage the Nuget packages for all projects in the solution, select the following from Visual Studio, Tools | NuGet Package Manager | Manage NuGet Packages for Solution... which is shown below:

The following dialog is displayed when Manage NuGet Packages for Solution... is selected:

From the previous screen click on the EntityFramwork which displays the following:

When EntityFramework is selected, the previous screen shows that the DataAccess project has Entity Framework 6.1.3 installed and that the MainApplication project does not have EntityFramework installed . By clicking on the check box next to the MainApplication it is possible to setup to install Entity Framework for the MainApplication, console application, project: 

When the check box next to MainApplication is checked, the Install button is enabled. Clicking on the Install button installs Entity Framework. A couple of dialog boxes are also displayed related to the package installed and licensing. The user should just navigate through these dialogs. 

The code is still not ready run. Attempting to run the console application, MainApplication, generates an exception as is show below:

The previous exception message is as follows: {"No connection string named 'SchoolEntities' could be found in the application config file."}

Recall the the wizard that setup a connection string within the app.config file for the class library, DataAccess. There is no suitable connection string in the app.config file of the console application. The remedy is simple; copy the <connectionStrings> section from the DataAccess project's app.config and paste it into the the app.config of the MainApplication project.

Lines 7-9 in the MainApplication's app.config were copied from DataAccess's app.config file. By making thi schange, the MainApplication console application will execute successfully. 

The source code for these projects can be found under: the EntityFramework6.Master solution folder within the following github,com repository: https://github.com/softwarepronto/Blog

No comments :

Post a Comment