简体   繁体   中英

Silverlight 4 LoadOperation returns null

LoadOperation on the client side returns null? How can I fix it? Is my code correct? Is it a best practice?

Serverside (Domain service:

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

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

Clientside

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 ********************/
}
}

Your question is not very clear as first you say that LoadOperation return null whereas in your code, you state that item.Country is null.

However, I believe that I see the problem.

In you Domain Service you call the Include("Country") method on the States EntityCollection. However, on client side, the State.Country Entity is still null? I had the same issue some time ago. It seems that RIA Services (or WCF) does not return those entities, unless you apply the [Include] attribute on the Entity you want to return like so in a metadata class

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

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

Someone will probably be able to give an explanation on why it works this way. I just know that I had to do it this way around :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM