Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access EntityObject type via RIA services

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
    {
       //..
    }
like image 652
Nemo Avatar asked Jan 22 '12 12:01

Nemo


1 Answers

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; }
like image 171
Zabavsky Avatar answered Oct 02 '22 01:10

Zabavsky