What is the best way to turn an EF mediation into a source POCO?

I have some POCO objects that I use in the context of EF Code First. Therefore, when I populate them with data, I am actually dealing with EF proxy objects, not the POCO itself.

I have an ASP.NET MVC4 ApiController that returns my POCO objects that I will use in my client application.

My "get" method looks something like this:

    // GET api/Clients/5
    public Client GetClient(int id)
    {
        Client client = db.Clients.Find(id);
        if (client == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }

        return client;
    }

This actually does not work, because when the serializer tries to serialize the Client object, it actually deals with the version of the EF proxy that causes its hiccups. See Can ApiController return an object with a collection of other objects?

So, I can turn off proxy generation by doing this with DbContext:

    db.Configuration.ProxyCreationEnabled = false;

, POCO, . Client , - EF, .

EF , POCO .

( ) ? , , , ?

+5
2

. : , : -, .

ASP.NET MVC, ( EF) , , . , UI, : , .

, asp.net web api, , - . 100% - .

, DTO ( POCO EF), 1:1.

, , CustomerDto.

, AutoMapper, DTO.

, .

+3

, , , , :

- POCO Windows Communication Foundation (WCF), DataContractSerializer . - . . POCO Proxies " POCO". POCO POCO, ProxyDataContractResolver - POCO .

http://msdn.microsoft.com/en-us/library/vstudio/ee705457(v=vs.100).aspx

+1

All Articles