实体框架数据合同 [英] Entity framework data contract

查看:75
本文介绍了实体框架数据合同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WCF和Entity框架的新手.

I am new to WCF and Entity framework.

我有一个包含"DatabaseEntities.edmx"(实体框架objectContext)的类库"A".

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

该库在内部使用实体对象公开了一个包含函数FunctionB的类"B".

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

我已将此库"A"引用带入WCF Web服务中,并位于IService.cs内-我已将其编码为:

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();

在没有定义任何DataContract的情况下,我进入了Service1.cs并实现了此功能 如下:

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(); 
}

这很好.

但是我的问题是:DataContract在WCF中是可选的吗?

But my question: is DataContract optional in WCF?

先谢谢了.

推荐答案

.NET 3.5 SP1起-是的,不再需要[DataContract].

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

如果省略它,则该类将像XML序列化器一样进行序列化:默认情况下,将对类上的所有公共属性进行序列化.

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.

但是:如果您开始在类中使用[DataContract]或在其中一个属性上使用[DataMember],则您必须装饰要使用[DataMember]序列化的所有属性.

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].

所以要么是:完全不使用那些属性(使用XML序列化程序所使用的默认行为),要么进行特定处理,并用[DataContract]装饰类,并使用[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.

这篇关于实体框架数据合同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆