I've migrated my blog

Thanks for visiting my blog. I am no longer maintaining this and I've migrated the blog to my personal domain . If you would like to follow my blog kindly use my new RSS feed

Friday, March 25, 2011

Entity Framework 4.0 - Class library - Part 1

In this blog post we are going to explore how to create the data access layer as a
class library using Entity Framework 4.0. By defining the data access layer as
class library, you can reuse it across many applications just by adding a
reference to the class library. In my next blog post, we'll explore how to consume
the library in an application

Entity Framework 4.0 enables the developer to
concentrate on the business logic and business objects without bothering how it
has been manipulated at the database level. You can find a very good tutorial on
Entity Framework
to get started.

This blog post uses a sample database named "HRD" which has two tables, Employee and Department.

Step 1: Create a Class Library

Create a new class library project with the name "HRD.DataAccess" and click "Ok"

After creating a new project delete the "Class1.cs" which is created by default.

Step 2: Create the Entity class using the entity framework

Right click on the project name "HRD.DataAccess" and select "Add -> New item".
Select "Data" under installed templates and then select "ADO.NET Data Entity Model".
Type the name as "HrdModel.edmx" and click "Add"

In the Entity Data Model Wizard select "Generate from database" and click "Next"

In the next step select the database name "PCName\sqlexpress.HRD.dbo" and select "Next"

In the next step select the tables names "Department" & "Employee" and click "Finish"

Now we have created the entity model which represent the Object/Relation mapping

Step 3: Building the class library

  1. Select "Build->Configuration Manager"
  2. Change the Configuration of the Project "HRD.DataAccess" to "Release". By default it is "Debug"
  3. Click "Close"
  4. Select "Build->Build HRD.DataAccess"

Thats it!!. Now we have the DataAccess Library ready!! We can reuse it across different .Net Applications


In this blog we have explored the way of creating the DataAccess class library using entity framework.
in my next blog, we'd explore on how to consume the data access library in an application.

1 comment: