Unable to access EntityObject type through RIA services

The My Entity Framework model is created from the SQL Server database. Since I need to access the database from Silverlight, I created a DomainService for RIAServices for the EF model. Product is one of the auto- EntityObject corresponding to the Product table. I am trying to pass a custom CompositeData class to a Silverlight client as shown. The problem is that the CurrentProduct field CurrentProduct not available in the client, but other string / int fields are available. How to make CurrentProduct available from the 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(); } } 

The following is the Domain Service method:

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

From 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 auto-generated in Model1.Designer.cs

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

It is also generated in the client project (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 { //.. } 
+6
source share
4 answers

You can define the relationship between CompositeData and Product using the Include and Association attributes.

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

(sorry for my bad english)

You need to expose your Product object in the DomainService class so that it can see it on the Silverlight side:

 public IEnumerable<Product> GetProduct() { //... return listProduct; } 
0
source

Here's what I do to quickly add tables to an RIA Silverlight project. this assumes that I already have an existing ADO.NET entity data model, DomainService.cs and DomainService.metadata.cs

  • update data model
  • build a project
  • add a whole new class and domain name, something other than the one you have.
  • add only the new table to your new domain service when it asks. this should generate both new domainervice.cs and domainservice.metadata.cs with information for your new table.
  • copy the automatically generated code from the new domain service and put it in your existing domain service and delete the one you just created.
  • do the same for metadata.
  • create a project and then do it.
0
source

This can be determined using the ExternalReferenceAttribute and AssociationAttribute attributes on the CurrentProduct property.

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

Just replace the Include attribute with the ExternalReference attribute.

0
source

Source: https://habr.com/ru/post/906582/


All Articles