2011-07-21 72 views
2

我遇到了使用Silverlight4,Ria服務和實體框架的問題。Ria服務和導航屬性問題

從我SL的客戶我試圖打通RIA Services的一些數據,在我的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的對象的導航屬性。 我可以訪問這些Promotie對象的屬性。

在silveright客戶端的方法。然而被調用時加載完成:

public void OnLoadEntitiesCompleted(ServiceLoadResult<T> result) { 

} 

在這種方法中,我得到的所有要求LastMinuteWachtLijstPromotie對象如預期,物業 Promotie不過是空的。

我已經設置的屬性的[包含]標記Promotie在自動生成的元數據類 和我使用.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

可以在添加元數據類,特別是對LastMinuteWachtLijstPromotie和Promotie? – Jehof

+0

在原文中添加了它們,tnx – Thomas

+0

您是否定義了NavigationProperty而不添加外鍵? – Jehof

回答

1

Composition屬性添加到LastMinuteWachtLijstPromotieMetadata類的屬性Promotie財產。那麼它應該工作。

public partial class LastMinuteWachtLijstPromotie { 
    internal sealed class LastMinuteWachtLijstPromotieMetadata{ 
    [Include] 
    [Composition] 
    public Promotie Promotie { get; set; } 
    } 
} 
+0

嗯,我已經添加了這個,重建然後測試它,它似乎沒有區別 – Thomas

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服務,我們希望它加載關聯: (注:智能感知說,這是一個圓點分隔的屬性名稱的列表,以包括,但是我嘗試類似.Include(「Category.SubCategory」),這失敗了一個例外...雖然.Include(「類別」)。包括(「子類別」)工作就像一個魅力!)

public IQueryable<Product> GetProducts() { 
    return this.ObjectContext.Products.Include("Category"); 
} 

我現在可以從Silverlight客戶端訪問我的「類別」屬性,它不是空:)

+0

添加[協會]沒有改變任何東西對我來說(SL5)。 – Timores

+0

同樣,我先使用代碼 – TChadwick

+0

當時我相信我正在創建使用.edmx和POCO類的混合映射的Inventory。 IIRC的經驗是一樣的。我很抱歉,自從我從事任何類似項目並且無法再訪問它已經很長時間了。請記住同時使用「密鑰」和「關聯」屬性,而不僅僅是「關聯」,如果沒有密鑰,則關聯無法解析。密鑰通常是可索引的(數字)。關聯有時可以基於其他類中的鍵,這是「IsForeignKey」發揮作用的地方。 – SilverX

1

同SilverX:剛剛這個問題,解決它,認爲它可能是有用的有人。

我也配置了所有東西(RIA S爲[Include],EF爲),但導航屬性在Silverlight端仍爲空。

原來,域服務方法使用的是[Invoke]屬性(並返回IEnumerable<T>)。刪除這個屬性解決了這個問題。

(只是備案,[調用]正被使用,因爲該方法具有List<Entity>參數)