我遇到了使用Silverlight4,Ria Services和Entity Framework的问题。
从我的sl客户端尝试通过ria服务获取一些数据,在我的domainService类中调用此方法:
public IQueryable<LastMinuteWachtLijstPromotie> GetLastMinuteWachtLijstPromoties(){
IQueryable<LastMinuteWachtLijstPromotie> list = (IQueryable<LastMinuteWachtLijstPromotie>)this.ObjectContext.LastMinuteWachtLijstPromoties.Include("Promotie");
return (from LastMinuteWachtLijstPromotie lwmp in list where lwmp.Actief select lwmp);
}
当我检查列表的内容时,在调试模式下,它填充了LastMinuteWachtLijstPromotie类型的对象。 这些对象具有名为Promotie的Object的导航属性。 我可以访问这些Promotie对象的属性。
在silveright客户端上,加载完成时会调用一个方法:
public void OnLoadEntitiesCompleted(ServiceLoadResult<T> result) {
}
在这个方法中,我按预期获得所有请求的LastMinuteWachtLijstPromotie对象,属性 然而,Promotie是空的。
我在自动生成的元数据类中的属性Promotie上设置了[Include]标签 我使用.Include(“Promotie”)
这些相同的方法用于我的域模型中的不同对象,这非常有效。 此外,我似乎无法在.edmx文件中找到数据库映射和导航属性的差异。
有没有人遇到过相同的问题或知道解决方案?
元数据类:
[MetadataTypeAttribute(typeof(LastMinuteWachtLijstPromotie.LastMinuteWachtLijstPromotieMetadata))]
public partial class LastMinuteWachtLijstPromotie
{
// This class allows you to attach custom attributes to properties
// of the LastMinuteWachtLijstPromotie class.
//
// For example, the following marks the Xyz property as a
// required property and specifies the format for valid values:
// [Required]
// [RegularExpression("[A-Z][A-Za-z0-9]*")]
// [StringLength(32)]
// public string Xyz { get; set; }
internal sealed class LastMinuteWachtLijstPromotieMetadata
{
// Metadata classes are not meant to be instantiated.
private LastMinuteWachtLijstPromotieMetadata()
{
}
public int AlertId { get; set; }
public string ArtikelNummer { get; set; }
public Nullable<int> ArtikelVariant { get; set; }
public int LastMinuteWachtLijstPromotieId { get; set; }
[Include]
public Promotie Promotie { get; set; }
public int PromotieArtikelId { get; set; }
public int PromotieId { get; set; }
public bool Actief { get; set; }
public DateTime Aanmaakdatum { get; set; }
}
}
[MetadataTypeAttribute(typeof(Promotie.PromotieMetadata))]
public partial class Promotie
{
// This class allows you to attach custom attributes to properties
// of the Promotie class.
//
// For example, the following marks the Xyz property as a
// required property and specifies the format for valid values:
// [Required]
// [RegularExpression("[A-Z][A-Za-z0-9]*")]
// [StringLength(32)]
// public string Xyz { get; set; }
internal sealed class PromotieMetadata
{
// Metadata classes are not meant to be instantiated.
private PromotieMetadata()
{
}
public string ActieType { get; set; }
public string AssortimentsManagerNaam { get; set; }
public string AssortimentsManagerTeamIds { get; set; }
[Display(Name = "Commerciele tekst")]
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Nokavision.ReclameFolder.UI.Web.Resources.ValidationResources))]
public string CommercieleTekst { get; set; }
[Display(Name = " ")]
public string CommercieleTekstDetails { get; set; }
[Include]
public Frame Frame { get; set; }
public Nullable<int> FrameId { get; set; }
public Nullable<DateTime> LastMinuteWijzigingsDatum { get; set; }
public string Opmerkingen { get; set; }
[Display(Name = "Op wachtlijst")]
public Nullable<bool> OpWachtLijst { get; set; }
//public Nullable<int> PromotieCopyId { get; set; }
public int PromotieId { get; set; }
[Include]
public EntityCollection<PromotieLeverancier> PromotieLeveranciers { get; set; }
[Include]
public EntityCollection<PromotieMutatie> PromotieMutaties{ get; set; }
//public Nullable<int> PromotieOrigineleId { get; set; }
[Include]
public EntityCollection<PromotieSymbool> PromotieSymbolen { get; set; }
public string Status { get; set; }
[Display(Name = "Promotie inhoud")]
public string PromotieInhoud { get; set; }
[Display(Name = "Promotie eenheid")]
public string PromotieEenheid { get; set; }
[Display(Name = "Promotie prijs")]
public decimal PromotiePrijs { get; set; }
}
}
答案 0 :(得分:1)
将Composition属性添加到LastMinuteWachtLijstPromotieMetadata类的属性Promotie属性中。然后它应该工作。
public partial class LastMinuteWachtLijstPromotie {
internal sealed class LastMinuteWachtLijstPromotieMetadata{
[Include]
[Composition]
public Promotie Promotie { get; set; }
}
}
答案 1 :(得分:1)
我知道这是一个较老的帖子,很可能已在其他地方得到解答,但我偶然发现它,因为没有人提供链接或更好的答案。
我目前正在使用Silverlight 5,这对我有用(我认为SL4 IIRC的流程是一样的)。
当向客户端推导导航属性时,您需要告诉RIA服务使用[Key]和[Association]属性在某处建立关系,这与实体框架不同,只是描述了如何将关系映射到正确的对象
首先是元数据类:
[MetadataTypeAttribute(typeof(Category.CategoryMetadata))]
public partial class Category
{
internal sealed class CategoryMetadata
{
private CategoryMetadata() {
}
[Key]
public int Id { get; set; }
public string NAME { get; set; }
[Association("CategoryToProducts", "Id", "CAT")]
[Include]
public EntityCollection<Product> Products { get; set; }
}
}
[MetadataTypeAttribute(typeof(Order.OrderMetadata))]
public partial class Order
{
internal sealed class OrderMetadata
{
// Metadata classes are not meant to be instantiated.
private OrderMetadata() {
}
[Key]
public int Id { get; set; }
public int PRODID { get; set; }
public DateTime DATE { get; set; }
public bool DONE { get; set; }
public int QTY { get; set; }
[Association("OrderToProduct", "PRODID", "Id", IsForeignKey = true)]
[Include]
public Product Product { get; set; }
}
}
[MetadataTypeAttribute(typeof(Product.ProductMetadata))]
public partial class Product
{
internal sealed class ProductMetadata
{
private ProductMetadata() {
}
[Key]
public int Id { get; set; }
public int CAT { get; set; }
public string NAME { get; set; }
public string DESC { get; set; }
public decimal PRICE { get; set; }
public int QTY { get; set; }
public long UPC { get; set; }
[Association("ProdToCat", "CAT", "Id", IsForeignKey = true)]
[Include]
public Category Category { get; set; }
[Association("ProductToOrders", "Id", "PRODID")]
[Include]
public EntityCollection<Order> Orders { get; set; }
}
}
现在我们需要告诉我们希望它加载关联的RIA服务: (注意:Intellisense说它是一个点分隔的属性名称列表,但是我尝试了类似.Include(“Category.SubCategory”)的东西,但是这个例子失败了......虽然.Include(“Category”)。包括( “SubCategory”)就像一个魅力!)
public IQueryable<Product> GetProducts() {
return this.ObjectContext.Products.Include("Category");
}
我现在可以从Silverlight客户端访问我的“Category”属性,它不是NULL:)
答案 2 :(得分:1)
与SilverX相同:只是遇到了问题,解决了它并认为它对某人有用。
我的所有配置内容都是正确的(RIA S为[Include]
,EF为Include()
但在Silverlight端导航属性仍然为null。
原来,域服务方法使用[Invoke]
属性(并返回IEnumerable<T>
)。删除此属性解决了这个问题。
(仅用于记录,正在使用[Invoke],因为该方法具有List<Entity>
参数)