none
Using EF generated Class as DataContract for WCF Service RRS feed

  • Question

  • Hi,

    I have an DB Entity(Employee) generated through database using Entity Framework in the form of EDMX. I want to use that DB Entity (Employee) as DataContract for my WCF Service. Please help me in providing best option for implementing the same.

    Below are the things I needed:

    1. Generate Employee DB Entity for Employee table where FName, LName are two columns in Employee table.

    2. Use Employee DB Entity as Data Contract for my WCF Service AddEmployee Operation.

    3. Add WCF Service Reference to my client.

    4. Update all properties of Employee DataContract at Client and call AddEmployee Operation.

    5. AddEmployee Operation should save employee data using EntityFramework SaveChanges() method.

    6. Here I want to skip the logic of mapping properties of Employee Data Contract to Employee DB Entity.

    Please help me asap.

    Thanks,

    Ravi Kiran V

    Monday, May 19, 2014 1:21 PM

Answers

  • Well, if you are using EF database first approach, then the EF entity class on the virtual model is an implicit DataContract to WCF, which you send through the WCF client and service as is.

    But on the other hand, most don't do that because the EF model classes must be seen by the WCF client and service too so that the client knows about the Entity/object, which you don't want to set reference to the EF model that is sitting behind the WCF service.

    That's why they have DTO(s) Data Transfer Objects that are used to send data across processes or through tiers in a n-tier solution.

    http://en.wikipedia.org/wiki/Data_Transfer_Object

    You map the EF entity to the DTO and send it to the WCF client, and the DTO is updated or a new DTO is created and populated on the client side. You send the DTO back to the WCF service that has methods usually in a classlib project called the DAL (Data Access Layer) the WCF service has reference to. The DAL takes the DTO passed in on a DAL method's signature, maps it back to an EF Entity and the Entity is persisted to the DB. 

    That's how you do it.

    The DTO(s) are put into a classlib project call it Entities and all projects have reference to Entities so that they can work with the DTO(s).

    A DTO made by Entity-2-DTO are implicit datacontracts to WCF based off of EF Entities on the EF model.

    http://visualstudiogallery.msdn.microsoft.com/655aa6d4-4461-42ea-aeec-64cdb1313de7

    You use the DTO(s), send the DTO(s) and you leave the EF Entities behind the WCF service. The EF entities never come past the WCF service. 

    Wednesday, May 21, 2014 2:39 AM