Cannot access EntityObject type via RIA services
Asked Answered
R

4

6

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
    {
       //..
    }
Ramillies answered 22/1, 2012 at 12:12 Comment(1)
Does you Silverlight client references an assembly, where the type Product is defined?Agulhas
P
1

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; }
Pyroligneous answered 9/2, 2012 at 13:26 Comment(0)
S
0

(sorry for my bad english)

You need to expose your Product entity in the DomainService class too to be able to see it on the silverlight side:

public IEnumerable<Product> GetProduct()
{
   //...
   return listProduct;
}
Sidero answered 23/1, 2012 at 3:4 Comment(2)
How can I expose? Is is already accessible in Domain Service class.Ramillies
You just need to put a method there returning the Product type or an IQueryable<Product> or IEnumerable<Product> (just like you did with the CompositeData class) - Without this, the problem you have is exactly what happens... but if it is already there, i can't imagine what could be happening:(Sidero
M
0

Here is what i do to quickly add tables to my RIA Silverlight project. this assumes i already have an existing ADO.NET Entity Data Model, DomainService.cs, and DomainService.metadata.cs

  1. i update my data model
  2. build project
  3. add a brand new Domain Service class and name is something different than the one you have.
  4. add only the new table to your new Domain Service when it asks. this should generate both a new domainservice.cs and a domainservice.metadata.cs with the info for your new table.
  5. copy out the auto generated code from the new domain service and place it in your existing domain service and delete the one you just created.
  6. do the same thing for the metadata.
  7. build the project and then your done.
Mesomorphic answered 31/1, 2012 at 8:20 Comment(0)
H
0

It is possible by defining ExternalReferenceAttribute and AssociationAttribute attribute over your CurrentProduct property.

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

Just replace Include attribute with ExternalReference attribute.

Harrell answered 13/2, 2012 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.