实体框架的WCF契约? [英] WCF Contracts from Entity Framework?
问题描述
这是Microsoft的Tim Mallalieu的答案。
实体类型在实体框架中生成的默认数据合约。
如果我要在Entity Designer中创建一个简单的模型,如下所示:
购物车实体类型默认为DataContract,所有属性都注释为数据成员。我们可以在WCF服务中使用它,如下所示:
[ServiceContract]
public interface IService1
{
[OperationContract]
购物车[] AllCarts();
}
public class Service1:IService1
{
public Cart [] AllCarts()
{
using(MSPetShop4Entities context = new MSPetShop4Entities())
{
var carts = from c in context.Carts select c;
return carts.ToArray();
}
}
}
由于实体是DataContracts现在可以按照您的要求滚动您的服务,并将其发送到电话上。
您可以轻松的方式使用 ADO.NET Data Services 。
I've been coming up with a lot of dead ends on this question. Supposedly, .NET 3.5 SP1 has Support for ADO.NET Entity Framework entities in WCF contracts. But when I look for solid info on it I'm not getting a lot of answers. I found this one snippet on an MSDN thread. Does anyone have any experience with this? What happened to the [DataContract]? Is this all there is to it? Why is there so little material on this?
This the answer from Tim Mallalieu in Microsoft.
Entity Types that are generated in the Entity Framework are, by default Data Contracts. If I were to create a simple model in the Entity Designer like the following: The cart Entity Type is by default a DataContract with all properties annotated as data members. We can then use this in a WCF service as follows:
[ServiceContract]
public interface IService1
{
[OperationContract]
Cart[] AllCarts();
}
public class Service1 : IService1
{
public Cart[] AllCarts()
{
using (MSPetShop4Entities context = new MSPetShop4Entities())
{
var carts = from c in context.Carts select c;
return carts.ToArray();
}
}
}
As the Entities are DataContracts you can now roll your services as you see fit and send these across the wire.
You could go the easy way and use ADO.NET Data Services.
这篇关于实体框架的WCF契约?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!