2013-12-18 39 views
0

我得到了一個OData服務,我試圖加載實體之間的關係。例如,我得到了一個Ponderation和TypePonderation實體。另一個ADO.Net OData主題

所以在我的客戶端,我想每個obtaint思考的typePonderation:

var ctx = new PonderationEntities(new Uri("http://localhost:7010/RDPPublication.svc")); 
var query3 = ctx.Ponderation.Expand("TypePonderation"); 

但是,當我在TypePonderation看,我有一個空引用,該typePonderation沒有加載。但是,如果我使用ctx.LoadProperty(VARIABLE,「TypePonderation」);如果我使用ctx.LoadProperty(VARIABLE,「TypePonderation」);

var query4 = ctx.Ponderation; 

      foreach (var VARIABLE in query4.ToList()) 
      { 
       Console.WriteLine(VARIABLE.CodeFonds); 

       ctx.LoadProperty(VARIABLE, "TypePonderation"); 

       Console.WriteLine(VARIABLE.TypePonderation.Code); 

       Console.WriteLine(); 
      } 

我得到了帶着我的思考的TypePonderation。

這是什麼錯誤?爲什麼展開不加載我的關係,但LoadProperty做到了這一點?

這裏是de Context的代碼(我使用edmx文件),所以代碼是自動生成的。

Entitie:Ponderation namespace CDP.RDP.DataAccess.Publication { using System; using System.Collections.Generic;

[Serializable()] 
    [DataContract(IsReference = true)] 
    public partial class Ponderation 
    { 
     public System.Guid PonderationID { get; set; } 
     public string CodeFonds { get; set; } 
     public string CodePortefeuille { get; set; } 
     public Nullable<System.Guid> TypePonderationID { get; set; } 
     public Nullable<System.DateTime> Periode { get; set; } 
     public Nullable<decimal> Ponderation1 { get; set; } 
     public Nullable<System.DateTime> DateCreation { get; set; } 
     public string CreePar { get; set; } 
     public Nullable<System.Guid> ReequilibragePubId { get; set; } 

     public virtual TypePonderation TypePonderation { get; set; } 
    } 
} 

Entitie TypePonderation

namespace CDP.RDP.DataAccess.Publication 
{ 
    using System; 
    using System.Collections.Generic; 

    [Serializable()] 
    [DataContract(IsReference = true)] 
    public partial class TypePonderation 
    { 
     public TypePonderation() 
     { 
      this.Ponderation = new HashSet<Ponderation>(); 
     } 

     public System.Guid TypePonderationID { get; set; } 
     public string Code { get; set; } 
     public string Description { get; set; } 
     public System.DateTime DateCreation { get; set; } 
     public string CreePar { get; set; } 

     public virtual ICollection<Ponderation> Ponderation { get; set; } 
    } 
} 

和上下文:

namespace CDP.RDP.DataAccess.Publication 
{ 
    using System; 
    using System.Data.Entity; 
    using System.Data.Entity.Infrastructure; 

    public partial class PonderationEntities : DbContext 
    { 
     public PonderationEntities() 
      : base("name=PonderationEntities") 
     { 
      this.Configuration.LazyLoadingEnabled = true; 
      this.Configuration.ProxyCreationEnabled = false; 
     } 

     public DbSet<TypePonderation> TypePonderation { get; set; } 
     public DbSet<Ponderation> Ponderation { get; set; } 
    } 
} 

謝謝。

回答

0

我找到了我的問題的答案。我爲了使它工作,我需要將我的關係包含在WCF數據服務中。

我探微添加此行的OnStartProcessingRequest了Methode:

CurrentDataSource.Ponderation.Include("TypePonderation").ToList(); 

並與該行我能與我的客戶端的方法展開工作。

這裏是我的代碼:

[ServiceBehavior(IncludeExceptionDetailInFaults = true)] 
    public class RDPPublication : DataService<CDP.RDP.DataAccess.Publication.RDPEntities> 
    { 
     //private IRDPPublicationServiceV2 _rdpPublicationService; 

     public RDPPublication() 
     { 
      //_rdpPublicationService = UIUnityRegistry.GetInstance().Resolve<IRDPPublicationServiceV2>(); 
     } 

     public static void InitializeService(DataServiceConfiguration config) 
     { 
      config.UseVerboseErrors = true; 
      config.MaxExpandCount = 300; 
      config.MaxExpandDepth = 300; 
      config.SetEntitySetAccessRule("*", EntitySetRights.AllRead | EntitySetRights.WriteMerge | EntitySetRights.WriteReplace); 
      config.SetServiceOperationAccessRule("*", ServiceOperationRights.AllRead); 
      config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; 
     } 

     protected override void OnStartProcessingRequest(ProcessRequestArgs args) 
     { 
      base.OnStartProcessingRequest(args); 

      CurrentDataSource.Ponderation.Include("TypePonderation").ToList(); 
     } 
    } 

希望這將有助於後來有人

安東尼

相關問題