That title is quite a mouthful. Let me try to be as clear as I can...
I have a WCF REST Service written in .NET 4 that uses the entity framework to pull some data from SQL Server into a list of objects. The objects are then returned as XML to the client. The problem is that the XML have references to each other due to my model's relationships.
Here is some code to help illustrate the problem:
My model: http://bara.stardock.com/images/activity_model.png
The Activities class that handles the service logic:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class Activities : IActivities
{
public ActivitiesList GetActivities(string titleId, string accountId, string numToReturn)
{
stardockActivitiesEntities sdActivitiesDb = new stardockActivitiesEntities();
int accountIdInt = int.Parse(accountId);
List<Activity> items = (from a in sdActivitiesDb.Activities
join ab in sdActivitiesDb.ActivityBridges
on a.ActivityID equals ab.ActivityID
where ab.AccountID == accountIdInt
select a).ToList();
ActivitiesList list = new ActivitiesList(items);
return list;
}
}
The interface for the above class:
[ServiceContract]
public interface IActivities
{
[OperationContract]
[WebGet(UriTemplate = "{titleId}/accounts/{accountId}/limits/{numToReturn}")]
ActivitiesList GetActivities(string titleId, string accountId, string numToReturn);
}
The Activity class is auto-generated by the entity framework based on my model for the Activities table. However, I did extend on this class by creating an ActivitiesList object:
[XmlRoot(Namespace = "http://schemas.datacontract.org/2004/07/Stardock.CVP.Stats")]
public partial class Activity
{
}
[XmlRoot(Namespace = "http://schemas.datacontract.org/2004/07/Stardock.CVP.Stats")]
[DataContract(IsReference=false)]
public class ActivitiesList
{
[DataMember]
public List<Activity> Activities { get; set; }
public ActivitiesList()
{
Activities = new List<Activity>();
}
public ActivitiesList(List<Activity> list)
{
Activities = new List<Activity>();
foreach (Activity item in list)
{
Activities.Add(item);
}
}
public void Add(Activity a)
{
Activities.Add(a);
}
}
So to explain my problem again, my XML, rather than simply returning a list of Activity like it should, instead returns a list of Activity with some activities referencing other activities that are within the base activities. That sounds confusing, but look at the images below:
Returned XML: http://bara.stardock.com/images/activity_xml1.png
The activity with a reference of "i8" is referring to another activity that is actually inside the activity with the id of "i2": http://bara.stardock.com/images/activity_xml2.png
My question is, how can I remove all of those extra relationships from the Activity object? I'd prefer it to just be a list of Activity without the nested ActivityType, EntityKey, etc. that are auto-generated by the entity framework.
I hope I've sufficiently explained myself. And if not, let me know what other details you'd like to see and I'll provide them.
Bara