Reputation: 1
i'm creating a webservice in C# using Entity Framework to retrive the data.
I have 2 related tables (Person, Role).
Each Person has a Role associated.
I would like that the returned objet is a Person and that the associated Role is included.
If I execute a query like this:
From p in ef.Person.Include("Role") select p
in debug I can see that the Role object for each Person is loaded.
But the returned/generated XML contains only a RoleReference wich gives me information about the related object, but not the object itself.
I tried to add the Serializable attribute to the classes generated by the entity framwork and also the xmlInclude attribute to the webmethod but nothing changes.
How can i solve this problem?
thanks!
Flavio
Upvotes: 0
Views: 807
Reputation: 1
Use a Partial class called Person that includes the Role nested object, or Role needed properties.
Upvotes: 0
Reputation: 3432
Are you passing the IEnumerable? Try a .ToList() to stop the lazy loading and pass that instead.
Upvotes: 1
Reputation: 6086
Create your own classes Person & Role and create for them mappers:
public class MyPerson
{
[DataMember]
public int ID {get; set;}
[DataMember]
public string Name {get; set;}
...
[DataMember]
public MyRole PersonRole {get; set;}
}
public class MyRole
{
[DataMember]
public int ID {get; set;}
...
}
Mapper:
MyRole Mapper(EFRole role)
{
return new MyRole() {
id = role.id, ...
}
}
MyPerson Mapper(EFPerson person)
{
return new MyPerson() {
id = person.id,
....
PersonRole = (person.Role != null ? Mapper(person.Role) : null;
}
}
Upvotes: 0