As a part of following best practice we need to separate the data access from our existing project. Here, we talk about how we create an ORM (Object-Relational Mapping) and create a separate DAL.
- Remove any Projects which might be unavailable.
- We must reconfigure our Connection String to the localhost within Web.Config file.
- Any connections to the TFS server shall be currently closed, because our development will be focused initially on local server.
- Build the Solution and execute it to check for any errors exist prior to the changes.
To begin with the process we add a class library to the existing EventNetworking (EN) web application.
- In the Solution Explorer tab, right click on ‘Solution’ and Add -> New Project
- Create a new ‘Class Library’ and name it “EventNetworking.Data”Make sure to change the target framework similar to the same version as the existing project. This is a very common mistake that a developer encounters.
- Right click on EventNetworking.Data->Properties. In our case we need to change the Target framework to 4.5Next we need to add an App.Config file and set the “ConnectionStrings”.
We might get the following Output when adding the App.Config
“Could not parse the configuration file. The error message is: ‘Could not find file ‘D:\Telerik\ZipFiles\EventNetworkCS\EventAdmin\Web.config’.’. Try editing the file manually and then saving it.”
We should copy the connection string from Web.Config file of the existing project.
Once we have done that, we again need to go to the Properties of new project and check the “Settings” tab to see if the connection string has been validated.
- Now we will transfer the existing data layer file to our new project. Transfer the “Networking.dbml” from “EventNetworking” project to EventNetworking.Data. DBML stands for DataBase Markup Language. The DBML file represents the data model for a LINQ to SQL project – it’s used to generate the C# code for the various entities. And build the “EventNetworking” project to get a list of errors. These errors have occurred because we have moved the DBML file to another project.
- To resolve the above errors, first we need to add a reference to the EN project.
Right click the “EventNetworking” and Add Reference -> Projects -> Select “EventNetworking.Data”.
Next, we will have to refactor the code as all the data links have been shifted from its current location to a new project.
Select “using EventNetworking.Data;” for all such errors.
Rebuild the project to remove errors depending on the above dependency.
- We create a new class within “EventNetworking.Data” named “ENData.cs” and copied all the dependent files from “Utilities.cs” within “EventNetworking”.
We also have to add references in the project for the following assemblies:
using System.Data.Services.Client; using System.Data.Entity;
- Remove Utilities from the EN project. Don’t forget to build the project and check for silly errors. Shortcut to build the “current project” is Ctrl+Shift+B. We would still see some error. These errors are produced because there is a discrepancy between the TaskList as a class and a file. So wherever we get an error saying such an element does not exist. Append the using statements with:using EventNetworking.Data;
using DataEntities = EventNetworking.Data;What these two lines would do is help differentiate between the TypeCollision caused by TaskList as file from the current project and TaskList as class from EventNetworking.Data. How to do that:
TaskList tl = new TaskList;
DataEntities.TaskList tl = new DataEntities.TaskList;
and make the above changes at all places where TaskList is being referred as class. Now we are left with around 20 errors and all of them having the same construct.
We will have to make these changes in more than one aspx file.
- Finally, we have resolved all the build time errors. The build is a success but there is a runtime error which still lingers.
This occurs because, we have moved the data access layer to a new project and modified the direct references. But we are still left with the most important reference which is mapping Linq to SQL.
To do so, change the property “ContextTypeName” from the “asp:LinqDataSource” control which is defined as:
Make changes to all the LinqDataSource control in all the aspx files. Build the project and we should be left with neither compile time nor runtime errors.
We have managed to create a separate DAL (Data Access Layer) and reproduce it into a new project named “EventNetworking.Data”. This blog post explains the various errors we faced while we moved the data layer from one project to another and how to resolve these issues.