Entity Framework WCF Contracts?

entity-framework wcf

Question

On this question, I've run across a number of dead ends. In WCF contracts, ADO.NET Entity Framework entities are reportedly supported by.NET 3.5 SP1. But I'm not finding many answers when I seek for reliable information about it. This particular tidbit was found in an MSDN discussion. Has anybody dealt with this before? How did the [DataContract] turn out? Is this the end of it? Why is there such a lack of information about this?

This is Tim Mallalieu's response from Microsoft.

Data Contracts are the default kind of entity type created by the Entity Framework. If I were to make a simple model in Entity Designer, it would look like this: All of the properties on the cart Entity Type's DataContract by default are marked as data members. The following is how we may utilize this in a WCF service:

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

You may now roll your services as you see fit and transmit them over the wire since the Entities are DataContracts.

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

Accepted Answer

You might use Data Services for ADO.NET to take the simple route.

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

Popular Answer

I advise against returning Entities just away. The DataContract for entities contains implementation-specific data, which is unfortunate. This won't work with other platforms, and it's the kind of thing that might have compatibility issues even between.NET versions.

I advise you to adhere to the Data Transfer Object design and just return POCO classes that are exact replicas of the data in the entities, devoid of any action. To represent a table, you may return a list of these classes, 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