Я смотрел на подобные вопросы, но пока не нашел решения.Я пытался использовать
.Include("")
.Load()
, но мои свойства навигации по-прежнему пусты, когда я получаю их через службу WCF.
Вот мои сущности:
public class Person
{
public int PersonID { get; set; }
public string Name { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public Person()
{
this.Addresses = new List<Address>();
}
}
public class Address
{
public int AddressID { get; set; }
public string AddressLine1 { get; set; }
public virtual ICollection<Person> Residents { get; set; }
public Address()
{
this.Residents = new List<Person>();
}
}
Используя свободный API, я заявляю, что обе сущности имеют HasMany адресов / резидентов соответственно.Мой код службы WCF выглядит следующим образом:
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class Repository : DataService<EFEntitiesDataContext>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
config.UseVerboseErrors = true;
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
protected override EFEntitiesDataContext CreateDataSource()
{
EFEntitiesDataContext dataSource = new EFEntitiesDataContext();
dataSource.Configuration.ProxyCreationEnabled = false;
dataSource.Configuration.LazyLoadingEnabled = true;
return dataSource;
}
[WebGet]
public IList<Person> GetAllPeople()
{
EFEntitiesDataContext context = this.CreateDataSource();
return context.People.Include("Addresses").Where(n => n.Addresses.Any()).ToList();
}
}
И, наконец, я вызываю свой сервис следующим образом:
static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:49479/Repository.svc");
RepositoryService.EFEntitiesDataContext repositoryService = new RepositoryService.EFEntitiesDataContext(uri);
Uri uriRequest = new Uri(string.Concat(repositoryService.BaseUri, "/GetAllPeople"));
foreach (var person in repositoryService.Execute<RepositoryService.Person>(uriRequest))
{
System.Console.WriteLine("Name: " + person.Name + ", Addresses: " + person.Addresses.Count.ToString());
}
System.Console.ReadKey();
}
Есть идеи у кого-нибудь?