2011-02-25 22 views
0

客户端上的LoadOperation返回null?我该如何解决它?我的代码是否正确?这是最佳做法吗?Silverlight 4 LoadOperation返回null

Serverside集团(域名服务:

public IQueryable<State> GetStates() 
{ 
return this.ObjectContext.States.Include("Country") ; 
} 

//----------------------------------------------------------------------- 

客户方

LoadOperation<State> loadOp; 
public IEnumerable<State> Entities() 
{ 
DSCommon _context = new DSCommon(); 
loadOp = _context.Load(_context.GetStatesQuery()); 
loadOp.Completed += complete; 
loadOp.Completed += new EventHandler(LoadOp_Completed); 
return loadOp.Entities; 
} 

EventHandler complete; 

void LoadOp_Completed(object sender, EventArgs e) 
{ 
foreach (var item in loadOp.Entities) 
{ 
/************* item.Country is Null ********************/ 
} 
} 

回答

2

你的问题是不是第一次你说LoadOperation返回null,而在你的代码很清楚,你声明item.Country is null。

但是,我相信我看到了th问题。

在您的域服务中,您可以在States EntityCollection上调用Include(“Country”)方法。但是,在客户端,State.Country实体仍然为空?前段时间我也有同样的问题。看来,RIA服务(或WCF)不返回的实体,除非你申请的[包含]属性的实体要在元数据类,像这样返回

[MetadataType(typeof(State.StateMetadata))] 
public partial class State 
{ 
    internal sealed class StateMetadata 
    { 
     private StateMetadata() 
     { 
     } 

     [Include] 
     public EntityCollection<Country> Country; 
    } 
} 

有人可能会能够给为什么它以这种方式工作的解释。我只知道我必须这样做:-)