Saturday, March 10, 2012

#10: WCF Service with Entity Framework

On my previous blog, we created a WCF service. This time I'll show you how to use a WCF service with the Entity Framework. First, we will create an ADO.Net Data Entity, then we will create a list collection Class in which we use to call the data in our Entity table.

In the solutions folder, right click on the project and click add new item. Under add new item window, select Ado.Net Entity Data model.



Under the data source, select which data provider. I am using a MySQL database, thus I used the MySQL Data provider.


Be sure to check on the Save entity connection setting in Web Config. This will generate a connection string on your web config.
Now select which table, views or stored procedure you want to use for this entity. Click finish and we have now a data entity model.


Here's how to use your entity model. First, let's create a class in which we will collect the data from our entity. In the solutions, right click on your project and add new item, select Class and name it TestDataClass.cs and follow the ff code;

 [DataContract ]
    public class TestDataClass
    {
        [DataMember]
        public DateTime TimeOff { get; set; }

        [DataMember]
        public string Cause { get; set; }

        [DataMember]
        public decimal Duration { get; set; }

        [DataMember]
        public decimal Customer { get; set; }

        [DataMember]
        public decimal TotalOutage { get; set; }

        [DataMember]
        public string UtilityName { get; set; }

        [DataMember]
        public int ID { get; set; }

    }


The above data member are the matching data on my entity table. You can create your own data member class depending on your table schema. I created this one so that I can create a list collection data which I will call it on the TestService.svc (we created the TestService.svc on the previous blog http://leeandotnet.blogspot.com/2012/03/9-wcf-service-application.html) 

Add the code below on the TestService.svc;

public List<TestDataClass> GetAllResultData()
        {
            List<TestDataClass> lstTestData = new List<TestDataClass>();
            using (aerinetdbEntities  aerinetDB = new aerinetdbEntities())
            {

                var id = from p in aerinetDB .testwcfs  
                              select p;
                foreach (testwcf b in id)
                {
                    TestDataClass a = new TestDataClass();
                    a.TimeOff = b.TimeOff.Value;
                    a.Cause = b.Cause;
                    a.Duration = b.Duration.Value;
                    a.Customer = b.Customer.Value;
                    a.TotalOutage = b.Customer.Value ;
                    a.UtilityName = b.UtilityName;
                    a.ID = b.id;

                    lstTestData.Add(a);
                }
            }
            return lstTestData;
        }



Then your ITestService, add this;

 [OperationContract]
        List<TestDataClass>  GetAllResultData ();

Build your WCF project and publish.

You can call this by adding the WCF to your service referrence and using the code below. Let's just say that we want to display this on our datagrid named dgvList.

             MyTestService = New AerinetWCFWebService.TestServiceClient() ;

             AerinetWCFWebService.TestDataClass  lstData  =  new AerinetWCFWebService.TestDataClass();
          
            dgvList.DataSource = AerinetService.GetAllResultData() 





No comments:

Post a Comment