实体框架通过 WCF 导航属性未填充

发布于 2024-12-29 03:39:49 字数 2513 浏览 1 评论 0原文

我看过类似的问题,但尚未找到解决方案。我尝试过使用

  1. .Include("")
  2. .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>();
    }
}

使用 Fluent API,我声明两个实体分别拥有许多地址/居民。我的 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();
}

大家有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2025-01-05 03:39:49

默认情况下不展开导航属性。并且服务器上没有办法强制它们扩展。客户端可以通过请求 /People?$expand=Addresses 轻松完成此操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文