I have a database, I mapped it via Ado.net entity model, now I want to expose a single class from my model via WCF service. How can it be achieved?
2 Answers
I don't know exactly which classes you'd created, but suppose you have a class named MyClass
. You'll need to add the attributes DataContract
and DataMember
like the following example:
[DataContract]
public class MyClass
{
[DataMember]
public Guid Id{ get; set; }
}
If you are using Entity Framework, that attributes are included by default. So, you don't need to add them to the entities generated by EF. You can check it by accessing the files generated inside the model.
An important thing to consider is that, if you don't use MyClass
as a parameter or you don't use it as a return, by the moment you generate service reference in your client app, this class won't appear.
Hope it helps!

- 43
- 7
Be VERY careful when doing this, EF classes don't generally play well with the serializer (and not at all if they have navigation properties).
A better approach would be to create a "clone" class that you make part of your data contract, and a translator class (or function) to go between the two. That way only the data you actually care about is exposed and you don't have to deal with the serializer freaking out on navigation properties.
Another good reason to not expose a EF generated class directly via WCF is that you don't necessarily want your contracts to change if an unimportant piece of the model changes.
This answer provides a good example of how to accomplish this.

- 1
- 1

- 60,462
- 10
- 96
- 117