简体   繁体   中英

How can I “transport” an entity object from a WCF service to a client application with Entity Framework?

I am completely new to Entitfy Framework. I am using a WCF service to pass data to an ASP.NET Web Forms application. I have classes that were generated according to the database schema I have.

First, I tried to get a simple result, executing an operation contract, named 'GetPublciations', which returns all the records, stored in my Publciation table, as an array of Publication objects. Everything compiled fine, but I got a run-time error. I fixed that by following the instructions in this question and its answer.

Basically, I added a line, like the one below in my method:

yourContextObject.Configuration.ProxyCreationEnabled = false; 

Now, I have a new problem. My Publication table has a foreign key CreationUserId, that references another table, named User. But when I try to get the value of some of the properties of the User object, it turns out that it actually has a value of NULL, which is imposible because in my database this foreign key column is NOT NULL. I tested this in a Console Application, without setting the ProxyCreationEnabled to false and then all of my "child" objects were created appropriately (meaning I had a User object inside my Publication object, which was not NULL).

I guess I can overcome this problem simply by creating a view in my database, for example, DetailedPublciation, then creating an appropriate auto-generated class with Entity Framework and then passing an object of this DetailedPublciation through my WCF service. Meaning, the newly created class will have only properties of primitive types like string, int, long, etc. and I won't have to reference a 'child' object.

So, my question is how I can fix that and whether creating a view like the mentioned above is the correct way to fix this problem.

Edit 1: I tried with this code inside my GetPublications method:

SomeEntities someEntities = new SomeEntities();
someEntities.Configuration.ProxyCreationEnabled = false;

return someEntities.Publications.Include("User").ToList();

I get a big exception 'tree', but the inner exception states:

An existing connection was forcibly closed by the remote host

Edit 2: I tried the following code in both - a Console Application and a WCF Service.

SomeEntities someEntities = new SomeEntities();
someEntities.Configuration.ProxyCreationEnabled = false;

return someEntities.Publications.Include(p => p.User).ToList();

In the Console Application everything works fine but in the service, I get a StackOverflow Exception.

ProxyCreationEnabled essentially turns off lazy loading which means you need to manually specify any extra objects you want retrieved from your database. Using Include for example, if your query looks like this:

var publications = from p in context.Publications
                   where p.Column = someValue
                   select p;

Then you should change it to this:

var publications = (from p in context.Publications
                   where p.Column = someValue
                   select p)
                       .Include(p => p.CreationUser);

The Include function will force Entity Framework to (eagerly) load the CreationUser object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM