ADO.NET Entity Framework 4.0: Simple N-Tier with Self-Tracking Entities and WCF

ADO.NET Entity Framework 4.0: Simple N-Tier with Self-Tracking Entities and WCF

Rate This
  • Comments 2

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

(

     [EmpId] ASC

) 

·        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.

namespace WcfServiceLibrary1
{
    [ServiceContract]
    public interface ITestDB
    {
        [OperationContract]
        Emp[] GetEmp();

        [OperationContract]
        Emp[] UpdateEmployee(Emp[] emps);        
    }
}

·     Now add one more class file TestDBService.cs with the following code,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using TestDBModel;

namespace WcfServiceLibrary1
{
    public class TestDBService : ITestDB
    {
        public Emp[] GetEmp()
        {
            TestDBEntities ctx = new TestDBEntities();
            return ctx.Emps.ToArray();
        }

        public Emp[] UpdateEmployee(Emp[] emps)
        {
            throw new NotImplementedException();
        }
        
    }
}

·         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,

private void button1_Click(object sender, EventArgs e)
{
    var svc = new ServiceReference1.TestDBClient();

    dataGridView1.DataSource = svc.GetEmp();
}

I also have added the code here (check below of this post if you have clicked the post title to read it)

Namoskar!!!

Attachment: NTier.zip
Leave a Comment
  • Please add 6 and 5 and type the answer here:
  • Post
  • Can you please explain why have you changed "Code Generation Strategy" to none?

  • How will you do CRUD operation from client side?if you have a collection of objects how will you pass the changed object only back the data layer?

Page 1 of 1 (2 items)