Generic Data Access Layer using ADO.NET Entity Framework

In this article, you will see how to create a generic data access layer (DAL) in a WCF proeject. I will use the ADO.NET Entity Framework to create a Data Model for the database. The Challenge is to pass the Entities to and from using a WCF Service.

If any of you have tried doing this before you would have faced the problem of serializing and de-serializing the entities. I will discuss the problems when we actually face them within the project.

Create a Data Access Layer

Let's start creating a generic data access layer (DAL).

In this article, I create a DAL and give some details about it. Since this is a detailed project, I will break down this article in a series of a few articles. 

Create a new Visual studio Project as shown below. 

Select Project Type Class Library.

Generic Dal using WCF

Once a Class Library project is created,  let's create a Data Model that will actually use as our Data Access Layer. We are using the ADO.NET Entity Data Model to create the data model for our database. 

Add a new Item and select ADO.NET Entity Data Model template from the selected templates. 

The next steps are self explanatory. Just follow the wizard steps. Select a database, connection string, select tables and other objects and so on.

Ado.Net Entity data Model


Select Generate from database option.


Ado.Net Entity data Model

Choose your data connection. If you do not have a data connection, click on New Connection and the wizard will guide you.


Ado.Net Entity data Model

This option will create a connection string. Next step, select your database objects - Tables, Views, and Stored Procedures.


Ado.Net Entity data Model


Once you are done, you will see the class diagram looks like following:


GenDalWCF6.gif

So our Data Model is created. This gives a clear picture of how the tables are associated. Now, we have an object model in the project, and every object in the database has a class associated with it. 

EntityModelCodeGenerator

Entity Framework offers a tool called EntityModelCodeGenerator. While we saw the UI being created. Behind the scenes, this tool creates a very elegant code behind for our database that maps the Data Model to the Database objects. 

DataContext Object is created as well. Using which we can access all the tables in the dataModel. We can also modify the tables i.e . Add, Update and Delete the tables and finally save the DataContext. Saving the Datacontext actually saves the data in the database.

GenDalWCF7.gif

Once the DataModel is created we can take a look at what code the Entity Framework has generated for us.

This is an Abstract View of what Entity Framework does for us. 

GenDalWCF8.gif

Let's check  the Context object. 

This is how the Context object is created.  The DataContext is the key to access any data.

GenDalWCF9.gif

The Below screenshot shows how the Entities are created .

GenDalWCF10.gif

Summary

In the first part of this series of articles, I demonstrates how to create an ADO.NET Data Entity data model for a database and objects. This model can be used to access and work with a database without writing a single line of code. 

In my next article, we will continue the same series.


Up Next
    Ebook Download
    View all
    Learn
    View all