Tuesday, August 23, 2011

EntityFramework and WCF

Recently while developing the WCF wrapper service for Entity Framework figured out that, the entities from EF cannot be used over the Channel. The reason was "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection". [You can capture this error if you enable WCF diagnostic logging].

There is a work around for this. You will have to use Data Transfer objects which mirror the entities from EF. So you can use those DTOs for tranmitting over the channel. It is not necessary that you will have to do this process manually, there are small add-ins available for doing this job. I used the EntitiesToDTO codeplex VS2010 add-in for this. So whenever we update the EF entities, just re-run this add-in which will update the DTOs based on the entities from EF.

PS - The transformation from entities to DTOs still need to be done by ourselves in the WCF operations.

1 comment:

  1. Hi Rajavel!
    I'm Fabian Fernandez, creator of EntitiesToDTOs. Thanks a lot for using the AddIn, I just released the new version which allows you to generate the Assemblers for DTOs so you don't have to do the mapping manually (at least not all the maping) with some other new features at http://entitiestodtos.codeplex.com .

    About what you said of Entities across the channel of WCF, I think that if you detach the entities from the context you could send them over the channel without problems. But I prefer to use the DTO Pattern so I don't reveal my model.

    Thanks again for spreading the word about EntitiesToDTOs !!!

    Happy new year!

    Regards,
    Fabian Fernandez

    ReplyDelete