2011-03-16 87 views
1

我有一個SL4/WCF RIA服務/ EF 4應用程序。我無法將我的Included實體放入我的SL4數據上下文中。實體框架/ RIA服務包括不工作

在本申請的服務器側服務部,這是我的方法:

[Query(IsDefault = true)] 
    public IQueryable<ToolingGroup> GetToolingGroups() 
    { 
     var groups = this.ObjectContext.ToolingGroups.Include("MetaData").OrderBy(g => g.Name); 
     return groups; //breakpoint set here 
    } 

我分配它到VAR組,以允許其在方法返回之前進行檢查。如果我的方法返回之前設置一個斷點,並添加一行到我的收藏窗口中的元數據有:

groups.First().MetaData 

當我讓方法的返回,並檢查它在Silverlight的用戶界面完成事件元數據爲空。

void loadOperation_Completed(object sender, System.EventArgs e) 
    { 
     grid.ItemsSource = _toolingContext.ToolingGroups; 
     UpdateUI(); //breakpoint set here 
    } 

當我做這在我的監視窗口元數據是空:

_toolingContext.ToolingGroups.First().MetaData 

我檢查,以確保通過調用。首先()在這兩種情況下返回ToolingGroup是相同的實體和它是。

爲什麼元數據在服務方法和我的UI方法之間丟失(例如空)?

SOLUTION:

// The MetadataTypeAttribute identifies ToolingGroupMetadata as the class 
// that carries additional metadata for the ToolingGroup class. 
[MetadataTypeAttribute(typeof(ToolingGroup.ToolingGroupMetadata))] 
public partial class ToolingGroup 
{ 

    // This class allows you to attach custom attributes to properties 
    // of the ToolingGroup 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 ToolingGroupMetadata 
    { 

     // Metadata classes are not meant to be instantiated. 
     private ToolingGroupMetadata() 
     { 
     } 

     public int Id { get; set; } 

     [Include] // Added so MetaData gets serialized 
     public MetaData MetaData { get; set; } 

     public Nullable<int> MetaDataId { get; set; } 

     public string Name { get; set; } 

     public ToolingCategory ToolingCategory { get; set; } 

     public int ToolingCategoryId { get; set; } 

     public EntityCollection<ToolingType> ToolingTypes { get; set; } 
    } 
} 

回答

4

有在玩兩層這裏,EF和RIA服務。你已經處理了EF部分。現在,您需要告訴RIA服務,在它通過線路串行化您的實體時包含該屬性。在實體的元數據中,添加[Include]屬性。像這樣...

[MetadataType(typeof(ToolingGroup.MetaData)] 
public partial class ToolingGroup { 
    private class MetaData { 

     // adding this attribute tells RIA services 
     // to also send this property across 
     [Include] 
     public MetaData MetaData { get; set; } 
    } 
} 

這是一個糟糕的巧合,你的類型被稱爲「元數據」,該ToolingGroup.MetaData類是RIA服務使用的元數據。

+1

不需要命名「元數據」類「元數據」!你可以自由地命名它,因爲它將設置在'MetadataTypeAttribute' :) – AbdouMoumen 2011-03-16 21:50:29

+0

是的,我注意到,當命名我的表時,MetaData是一個糟糕的選擇 - 當然事實上。但是 - 我沒有看到添加[Include]屬性的位置。 – 2011-03-16 21:56:18

+0

你可以看看這個截圖,看看我是否在正確的位置添加屬性? http://img855.imageshack.us/i/include.png/否則,我的解決方案中看不到任何類似於您的示例的代碼。 – 2011-03-16 22:01:34