实体框架通过 WCF 导航属性未填充
我看过类似的问题,但尚未找到解决方案。我尝试过使用
.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>();
}
}
使用 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
.Include("")
.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下不展开导航属性。并且服务器上没有办法强制它们扩展。客户端可以通过请求 /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.