简体   繁体   中英

Entity Framework Through WCF Navigation Properties Not Populating

I've had a look at the similar questions, but haven't found a solution yet. I've tried using

  1. .Include("")
  2. .Load()

but my navigation properties are still empty when I retrieve them through my WCF service.

Here are my entities:

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>();
    }
}

Using the fluent API I am declaring that both entities HasMany of addresses / residents respectively. My WCF service code looks like this:

[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();
    }
}

And finally, I am invoking my service like this:

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();
}

Any ideas anyone?

Navigation properties are not expanded by default. And there's no way on the server to force them to expand. The client can do so easily by requesting /People?$expand=Addresses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM