通过RIA服务无法访问EntityObject类型 [英] Cannot access EntityObject type via RIA services

查看:107
本文介绍了通过RIA服务无法访问EntityObject类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实体框架模型是从SQL Server数据库中生成的。因为我需要从Silverlight中访问数据库,我产生一个DomainService的反对的EF模型RIAServices。 产品是自动生成的 EntityObject 对应表中的一个产品。我试图穿过通过自定义类的CompositeData 到Silverlight客户端,如图所示。问题是, CurrentProduct 字段是不是在客户端访问,但另一个字符串/ int字段的访问。如何才能使从客户 CurrentProduct 访问?

 公共类的CompositeData
{
    [键]
    公众的Guid p键{获得;组; }
    公共字符串CompositeName {获得;组; }
    公共字符串标识{获取;组; }
    公共产品CurrentProduct {获得;组; } //产品是自动生成的EntityObject类

    公众的CompositeData()
    {
        PKEY = Guid.NewGuid();
    }
}
 

以下是域名服务的方法:

  [EnableClientAccess()]
公共类LocalDomainService:DomainService的
{
   公开的IEnumerable<的CompositeData>的GetData()
   {
       名单<的CompositeData>的ListData =新的名单,其中,的CompositeData>();
       // ...
       返回的ListData;
   }
}
 

从Silverlight客户端,

  domService.Load(domService.GetDataQuery(),GetDataCompleted,NULL);

    私人无效GetDataCompleted(LoadOperation<的CompositeData> compData)
    {
        的foreach(的CompositeData cdItem在compData.Entities)
        {
            // cdItem.CompositeName访问
            // cdItem.CurrentProduct是无法访问!
        }
    }
 

编辑: 产品类是自动生成的Model1.Designer.cs

  [EdmEntityTypeAttribute(NamespaceName =MyDBModel,NAME =产品)]
    [序列化()]
    [DataContractAttribute(IsReference =真)
    公共部分类产品:EntityObject
    {
        // ..
    }
 

它获取客户端项目还产生(在SilverlightProject.g.cs)

  ///<总结>
    ///产品实体类。
    ///< /总结>
    [DataContract(命名空间=htt​​p://schemas.datacontract.org/2004/07/SilverlightProject)
    公共密封部分类产品:实体
    {
       // ..
    }
 

解决方案

您可以定义的的CompositeData 的关系和产品使用包含协会属性。

  [System.ServiceModel.DomainServices.Server.Include]
[System.ComponentModel.DataAnnotations.Association(AssociationName,MainKey,AssociatedObjectKey)]
公共产品CurrentProduct {获得;组; }
 

My Entity Framework model is generated from SQL Server database. Since I need to access database from Silverlight, I generated a DomainService for RIAServices against the EF model. Product is one of the autogenerated EntityObject corresponding to the table Product. I am attempting to pass the custom class CompositeData across to the Silverlight client as shown. The problem is that CurrentProduct field is not accessible in the client but the other string/int fields are accessible. How can make CurrentProduct accessible from client?

public class CompositeData
{
    [Key]
    public Guid PKey { get; set; }
    public string CompositeName { get; set; } 
    public string Identity { get; set; }
    public Product CurrentProduct { get; set; }  //Product is an auto-generated EntityObject class

    public CompositeData()
    {
        PKey = Guid.NewGuid();
    }
}

Following is the Domain Service method:

[EnableClientAccess()]
public class LocalDomainService : DomainService
{
   public IEnumerable<CompositeData> GetData()
   {
       List<CompositeData> listData = new List<CompositeData>();
       //...
       return listData;
   }
}

From the Silverlight client,

    domService.Load(domService.GetDataQuery(), GetDataCompleted, null);

    private void GetDataCompleted(LoadOperation<CompositeData> compData)
    {
        foreach(CompositeData cdItem in compData.Entities)
        {
            // cdItem.CompositeName is accessible
            // cdItem.CurrentProduct is not accessible!
        }                     
    }

EDIT: Product class is autogenerated in Model1.Designer.cs

    [EdmEntityTypeAttribute(NamespaceName="MyDBModel", Name="Product")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class Product : EntityObject
    {
        //..
    }

It gets generated in the client project also (in SilverlightProject.g.cs)

    /// <summary>
    /// The 'Product' entity class.
    /// </summary>
    [DataContract(Namespace="http://schemas.datacontract.org/2004/07/SilverlightProject")]
    public sealed partial class Product : Entity
    {
       //..
    }

解决方案

You can define a relation between CompositeData and Product using Include and Association attributes.

[System.ServiceModel.DomainServices.Server.Include]
[System.ComponentModel.DataAnnotations.Association("AssociationName", "MainKey", "AssociatedObjectKey")]
public Product CurrentProduct { get; set; }

这篇关于通过RIA服务无法访问EntityObject类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆