ADO.NET Entity Framework 4.0 allows us to easily create N-Tier Application with the help of Self-Tracking Entities. This means now we do not have to worry about merging and checking values in more disconnected scenario. Here is how we can do it in very simple example to start with.
Create Model Layer
· Create a blank solution in Visual Studio 2010 with the name NTier.
· Create a new Class Library application. Give the name TestDBModel.
· Right click to the TestDBModel project and choose Add New Item. Add ADO.NET Entity Data Model and name it “TestDBModel.edmx”.
· Point to the database and table (here it is Emp)
· The Emp table is simple and has
CREATE TABLE [dbo].[Emp](
[EmpId] [int] IDENTITY(1,1) NOT NULL,
[EmpName] [varchar](50) NULL,
CONSTRAINT [PK_Emp] PRIMARY KEY CLUSTERED
· Then select the TestDBModel.edmx from the designer and go to the property window and select the property “Code Generation Strategy” and change it to None. The reason behind doing it is to get WCF enabled entity classes which can keep the track of changes made across the layers without having an open connection (context).
· Then right click on the TestDBModel.edmx designer and select “Add Code Generation Item”.
· Choose “ADO.NET Self-Tracking Entity Generator” and name it Model1.tt.
· If you explore it you will find the context and Emp class created by that template already. You do not have to write/change anything there.
Create Service Layer
· Now create a WCF Class Library project with a name WcfServiceLibrary1.
· Add a class file ITestDB.cs with the following code.
· Now add one more class file TestDBService.cs with the following code,
· You also need to bring the EF connection string from the model project to this project’s App.config file.
Create UI Layer
· Create a Windows Forms Application with a name WindowsFormsApplication1. Add Service reference to your WCF Class Library.
· Then add Grid View named dataGridView1 and Button named button1.
· Add the following code,
I also have added the code here (check below of this post if you have clicked the post title to read it)