Entity Framework and WCF navigational property

Asked

Viewed 115 times

0

I am trying to implement a WCF service that will return a Model that I also use in the Entity Framework. When the model has a property virtual from navigation to association with another model WCF cannot parse the object and ends up returning an error saying that the connection has been closed.

When I remove the reference (navigation property) the parse is successfully done and the object is returned.

What’s the right way to do this? Can’t I directly return a Model? How do I deal with these association properties in WCF?

  • Thiago, good afternoon, I’m the same problem, you got some solution?

  • Good Afternoon Ricardo and Welcome to Sopt, the comments are a field of clarification of doubts, in case you have a question I recommend you ask a new question so that the community can help you directly.

  • @Ricardopulini, the Entity Framework needs the property to be virtual in order to do the LazyLoading, but it is not desirable to put the [DataMember] in a virtual property, in this case, the best thing to do is to create a DTO and do the Mapping of Entity to the DTO and move on to DTO.

1 answer

0

You tried to remove the virtual of the navigation property, when using the virtual, indicates that we are performing with Lazy Loading enabled. It is not very logical to use this concept in web applications, because in the client we will always need all the data.

This article explains the concept of Lazy Loading in more detail: http://geekswithblogs.net/danemorgridge/archive/2010/05/04/entity-framework-4-wcf-amp-lazy-loading-tip.aspx

Another factor also commented in the article above is the use of Entity Framework proxy. By default, the Entity Framework creates Proxy classes to perform the conversion of the models to the database and this hinders the serialization of the data for transmission via WCF.

Also check this Stack Overflow gringo post: https://stackoverflow.com/questions/4884420/wcf-and-entities-framework-lazy-loading.

I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.