views:

31

answers:

1

Right now i'm doing some tests involving entityFramework and WCF. As I understand, the EntityObjects generated are DataContracts and so, they can be serialized to the client.

In my example I have a "Country" entity wich has 1 "Currency" as a property, when I get a Country and try to send it to the client, it throws an exception saying the data cant be written.

But, the thing is, if I Get a Currency (which has a collection of Countries) and dont load its countries, it does work. The client gets all the entities.

So, as a summary: - I have an entity with another entity as a property and cant be serialized. - I have another entity with an empty list of properties and it is successfully serialized.

Any ideas on how to make it work?

A: 

Entity Framework by default does not automatically load associated entities, e.g. if you load your "Country" entity, by default and unless you do something, the associated "Currency" will not be loaded.

What you need to do is either do an .Include("Currency") in your EF query, or load the associated Currency property manually. It's a 1:1 relationship, right? In that case, your Country entity will most likely contain a member called CurrencyReference and you can check whether that's loaded or not, and if not, you can manually load the associated entity:

if(!myCountry.CurrencyReference.IsLoaded)
{
   myCountry.CurrencyReference.Load();
}

When you do this, and then serialize the object to be sent over WCF - does it work now?

Marc

marc_s
i think he is saying it only happens when he loads and entity and tries to send it...
Nix