Entity framework data contract

做~自己de王妃 提交于 2020-01-23 10:46:12

问题


I am new to WCF and Entity framework.

I have a class library "A" which contains DatabaseEntities.edmx (Entity Framework objectContext).

This library is exposing a class "B" which contains a function FunctionB, internally using the entity objects.

I have taken this library "A" reference into a WCF web service and inside the IService.cs - I have coded it like this:

[OperationContract]
void FunctionB_Proxy();

Without defining any DataContract I have gone into Service1.cs and implemented this function as below:

public void FunctionB_Proxy()
{
  ClassB x=new ClassB();//Class of ClassLibrary
  x.FunctionB(); 
}

This works fine.

But my question: is DataContract optional in WCF?

Thanks in advance..


回答1:


As of .NET 3.5 SP1 - yes, the [DataContract] is no longer required.

If you omit it, then the class will be serialized like the XML serializer does: all public properties on your class will be serialized by default.

However: if you start using a [DataContract] on your class or a [DataMember] on one of your properties, then you must decorate all properties you want to serialize with a [DataMember].

So it's either : leave out those attributes entirely (use the default behaviour like the XML serializer uses), or then be specific and decorate the class with [DataContract] and every property you want to have serialized with [DataMember]. I prefer this second approach.



来源:https://stackoverflow.com/questions/16881450/entity-framework-data-contract

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!