Entity Framework WCF Contracts?

entity-framework wcf

Question

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.

1
10
2/25/2009 2:56:43 PM

Accepted Answer

You could go the easy way and use ADO.NET Data Services.

1
2/25/2009 6:29:55 PM

Popular Answer

I recommend that you not return Entities directly. Unfortunately, Microsoft chose to include implementation-specific data as part of the DataContract for entities. This will not interoperate with other platforms, and is the sort of thing that might fail to interoperate even between .NET versions.

Instead, I recommend you follow the Data Transfer Object pattern and just return POCO classes that are copies of the data in the entities, with no behavior. You can return List of such classes to represent a table, etc.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow