I’m using Entity framework 4.1. I need to create a WCF service which needs fetches from an existing database. I am using wsHttp binding. I have used the DbContext generator with WCF support. The following are two of the classes that have been generated:
[DataContract(IsReference = true)]
[KnownType(typeof(TestSummary))]
public partial class Test
{
[DataMember]
public int Id { get; private set; }
[DataMember]
public virtual TestSummary TestSummary { get; set; }
}
and,
[DataContract(IsReference = true)]
[KnownType(typeof(Test))]
public partial class TestSummary
{
[DataMember]
public int Id { get; private set; }
[DataMember]
public string Summary { get; set; }
[DataMember]
public virtual Test Test { get; set; }
}
When I fetch all the Test objects, I do not get the TestSummary related to it. The same happens with all my other entities. Code use to fetch the Test entity:
List<Test> tests = new List<Test>();
using (TestModelContainer context = new TestModelContainer())
{
var query = (from t in context.Tests select t);
foreach (var t in query)
{
Test test = (Test)t;
tests.Add(test);
}
}
However, when I generate the classes using the DbContext generator without WCF support, all the related entities are also loaded (I tested it with a console application).
in your query, you are not fetching related
TestSummaryentities. You need to declare explicitly that you want to fetch related TestSummaries with adding anIncludestatement to your query.Also, there is no need to create
List<Test> tests; you may just return what you have fetched from the query;