1
我已經看過類似的問題,但還沒有找到解決方案。我已經使用通過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>();
}
}
使用流利的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();
}
任何想法嗎?
服務器端(在'GetAllPeople()')中還是僅在客戶端(在'Main'中)地址已經丟失?順便說一句:我認爲設置'ProxyCreationEnabled = false'和'LazyLoadingEnabled = true'沒有意義,因爲延遲加載不能沒有代理工作。但是它可能與你的問題沒有任何關係,因爲你正在使用'Include'進行加載。 – Slauma