[英]Cannot access EntityObject type via RIA services
我的实体框架模型是从SQL Server数据库生成的。 由于我需要从Silverlight访问数据库,因此我针对EF模型为RIAServices生成了一个DomainService。 Product
是与表Product
对应的自动生成的EntityObject
之一。 我试图将自定义类CompositeData
传递给Silverlight客户端,如图所示。 问题是客户端无法访问CurrentProduct
字段,但可以访问其他字符串/ int字段。 如何从客户端访问CurrentProduct
?
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();
}
}
以下是域服务方法:
[EnableClientAccess()]
public class LocalDomainService : DomainService
{
public IEnumerable<CompositeData> GetData()
{
List<CompositeData> listData = new List<CompositeData>();
//...
return listData;
}
}
从Silverlight客户端,
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!
}
}
编辑: Product
类在Model1.Designer.cs中自动生成
[EdmEntityTypeAttribute(NamespaceName="MyDBModel", Name="Product")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Product : EntityObject
{
//..
}
它也在客户端项目中生成(在SilverlightProject.g.cs中)
/// <summary>
/// The 'Product' entity class.
/// </summary>
[DataContract(Namespace="http://schemas.datacontract.org/2004/07/SilverlightProject")]
public sealed partial class Product : Entity
{
//..
}
您可以使用Include
和Association
属性定义CompositeData
和Product
之间的关系。
[System.ServiceModel.DomainServices.Server.Include]
[System.ComponentModel.DataAnnotations.Association("AssociationName", "MainKey", "AssociatedObjectKey")]
public Product CurrentProduct { get; set; }
(对不起,我的英语不好)
您还需要在DomainService类中公开您的Product
实体,以便能够在Silverlight端看到它:
public IEnumerable<Product> GetProduct()
{
//...
return listProduct;
}
以下是我快速添加表到我的RIA Silverlight项目的方法。 假设我已经有一个现有的ADO.NET实体数据模型,DomainService.cs和DomainService.metadata.cs
可以通过CurrentProduct属性定义ExternalReferenceAttribute和AssociationAttribute属性。
[System.ServiceModel.DomainServices.ExternalReference]
[System.ComponentModel.DataAnnotations.Association("AssociationName", "MainKey", "AssociatedObjectKey")]
public Product CurrentProduct { get; set; }
只需将Include属性替换为ExternalReference属性即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.