Entity framework 在Silverlight中的ADO.Net数据服务中批量加载属性

Entity framework 在Silverlight中的ADO.Net数据服务中批量加载属性,entity-framework,silverlight-3.0,wcf-data-services,Entity Framework,Silverlight 3.0,Wcf Data Services,我在Silverlight 3中使用ADO.Net数据服务(Astoria),我想将实体上的属性加载延迟到初始加载之后。但是,当我准备加载它们时,我希望将加载请求一起批处理 // this is what I want to avoid var c = (from c in ctx.Customers.Expand("Address,Phone,Email") where c.Id = 12 select c).Take(1) as DataServiceQu

我在Silverlight 3中使用ADO.Net数据服务(Astoria),我想将实体上的属性加载延迟到初始加载之后。但是,当我准备加载它们时,我希望将加载请求一起批处理

// this is what I want to avoid
var c = (from c in ctx.Customers.Expand("Address,Phone,Email")
         where c.Id = 12
         select c).Take(1) as DataServiceQuery<Customer>;

LoadProperty方法不使用dataservice中提供的任何标准类型。然而,数据服务的智能程度足以说明这一点

LoadProperty(person, "Gender")

person.Gender = (from g in ent.Person
                 where g.ID == person.ID
                 select g.Gender).FirstOrDefault();
生成的Uri是相同的

http://localhost/WebDataService.svc/Person(1)/Gender
因此,如果要在批处理查询中调用LoadProperty,可以非常轻松地生成所需的Uri。见下文

public static class DataServiceContextExtensions
{
    public static Uri GetLoadPropertyUri(this DataServiceContext context, object entity, string property)
    {
        Uri entityUri = null;
        if(context.TryGetUri(entity, out entityUri))
        {
            return new Uri(entityUri.AbsoluteUri + "/" + property);
        }
        throw new DataServiceClientException("Entity Uri not found.");
    }

    public static DataServiceRequest<T> GetLoadPropertyRequest<T>(this DataServiceContext context, object entity, string property)
    {
        return new DataServiceRequest<T>(context.GetLoadPropertyUri(entity, property));
    }
}
公共静态类DataServiceContextExtensions
{
公共静态Uri GetLoadPropertyUri(此DataServiceContext上下文、对象实体、字符串属性)
{
Uri entityUri=null;
if(context.TryGetUri(entity,out entityUri))
{
返回新的Uri(entityUri.AbsoluteUri+“/”+属性);
}
抛出新的DataServiceClientException(“未找到实体Uri”);
}
公共静态DataServiceRequest GetLoadPropertyRequest(此DataServiceContext上下文、对象实体、字符串属性)
{
返回新的DataServiceRequest(context.GetLoadPropertyUri(实体,属性));
}
}
现在你可以这样做了

ctx.BeginExecuteBatch(BatchCallback, objState, new []{
    ctx.GetLoadPropertyRequest<Address>(c, "Address"),
    ctx.GetLoadPropertyRequest<Phone>(c, "Phone"),
    ctx.GetLoadPropertyRequest<Email>(c, "Email"),
    GetOtherNonPropertyQuery()
});
ctx.BeginExecuteBatch(BatchCallback,objState,new[]{
ctx.GetLoadPropertyRequest(c,“地址”),
ctx.GetLoadPropertyRequest(c,“电话”),
ctx.GetLoadPropertyRequest(c,“电子邮件”),
GetOtherNonPropertyQuery()
});
剩下的唯一一件事是,它只会将对象返回给您,而不会将返回值赋给entity属性,这将需要您自己在BatchCallback上执行

不管怎样,彼得希望这对你有所帮助

如果你需要什么,请告诉我

问候


丹尼尔

非常感谢这正是我想要的。
public static class DataServiceContextExtensions
{
    public static Uri GetLoadPropertyUri(this DataServiceContext context, object entity, string property)
    {
        Uri entityUri = null;
        if(context.TryGetUri(entity, out entityUri))
        {
            return new Uri(entityUri.AbsoluteUri + "/" + property);
        }
        throw new DataServiceClientException("Entity Uri not found.");
    }

    public static DataServiceRequest<T> GetLoadPropertyRequest<T>(this DataServiceContext context, object entity, string property)
    {
        return new DataServiceRequest<T>(context.GetLoadPropertyUri(entity, property));
    }
}
ctx.BeginExecuteBatch(BatchCallback, objState, new []{
    ctx.GetLoadPropertyRequest<Address>(c, "Address"),
    ctx.GetLoadPropertyRequest<Phone>(c, "Phone"),
    ctx.GetLoadPropertyRequest<Email>(c, "Email"),
    GetOtherNonPropertyQuery()
});