[英]Entity Framework / RIA Services Include not working
我有一個SL4 / WCF RIA服務/ EF 4應用程序。 我無法將包含的實體放入SL4數據上下文中。
在應用程序的服務器端服務部分,這是我的方法:
[Query(IsDefault = true)]
public IQueryable<ToolingGroup> GetToolingGroups()
{
var groups = this.ObjectContext.ToolingGroups.Include("MetaData").OrderBy(g => g.Name);
return groups; //breakpoint set here
}
我將它分配給var組以允許在方法返回之前檢查它。 如果我在方法返回之前設置斷點並在Watch窗口中添加一行,那么MetaData就在那里:
groups.First().MetaData
當我讓方法返回並在silverlight ui完成事件中檢查它時MetaData為null。
void loadOperation_Completed(object sender, System.EventArgs e)
{
grid.ItemsSource = _toolingContext.ToolingGroups;
UpdateUI(); //breakpoint set here
}
當我在監視窗口中執行此操作時,MetaData為null:
_toolingContext.ToolingGroups.First().MetaData
我檢查過以確保在兩種情況下通過調用.First()返回的ToolingGroup都是同一個實體。
為什么MetaData在服務方法和我的ui方法之間丟失(例如null)?
解:
// 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; }
}
}
這里有兩層,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服務使用的元數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.