实体框架的WCF契约? [英] WCF Contracts from Entity Framework?

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

问题描述

我在这个问题上遇到了很多死胡同。据说.NET 3.5 SP1在WCF契约中支持ADO.NET实体框架实体。但是当我找到坚实的信息时,我没有得到很多答案。我在MSDN线程上发现了这个片段。有谁有任何经验吗? [DataContract]发生了什么?这是一切吗?为什么这么少的材料?



这是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屋!

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