实体框架/RIA 服务包括不工作
我有一个 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 组,以允许在方法返回之前检查它。如果我在方法返回之前设置断点并向监视窗口添加一行,则元数据就在那里:
groups.First().MetaData
当我让方法返回并在 silverlight ui 完成事件中检查它时,元数据为 null。
void loadOperation_Completed(object sender, System.EventArgs e)
{
grid.ItemsSource = _toolingContext.ToolingGroups;
UpdateUI(); //breakpoint set here
}
当我在监视窗口中执行此操作时,元数据为空:
_toolingContext.ToolingGroups.First().MetaData
我检查以确保在两种情况下调用 .First() 返回的 ToolingGroup 是相同的实体,而且确实如此。
为什么元数据在服务方法和我的 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; }
}
}
I've got a SL4 / WCF RIA Services / EF 4 application. I'm having trouble getting my Included entity into my SL4 data context.
In the server side service portion of the application, this is my method:
[Query(IsDefault = true)]
public IQueryable<ToolingGroup> GetToolingGroups()
{
var groups = this.ObjectContext.ToolingGroups.Include("MetaData").OrderBy(g => g.Name);
return groups; //breakpoint set here
}
I assigned it to the var groups to allow it to be inspected before the method returns. If I set a breakpoint before the method returns and add a line to my Watch window the MetaData is there:
groups.First().MetaData
When I let the method return and check it in the silverlight ui completed event MetaData is null.
void loadOperation_Completed(object sender, System.EventArgs e)
{
grid.ItemsSource = _toolingContext.ToolingGroups;
UpdateUI(); //breakpoint set here
}
When I do this in my watch window MetaData is null:
_toolingContext.ToolingGroups.First().MetaData
I checked to make sure the ToolingGroup returned by the call to .First() in both cases was the same entity and it was.
Why is MetaData lost (eg. null) between the service method and my ui method?
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; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有两个层在起作用:EF 和 RIA 服务。您已经处理了 EF 部分。现在,您需要告诉 RIA 服务在通过网络序列化您的实体时包含该属性。在实体的元数据中,添加
[Ininclude]
属性。像这样...您的类型被称为“元数据”,这是一个糟糕的巧合,ToolingGroup.MetaData 类是 RIA 服务使用的元数据。
There are two layers at play here, EF and RIA Services. You've handled the EF part. Now you need to tell RIA services to include that property when it serializes your entities across the wire. In your metadata for the entity, add the
[Include]
attribute. Like this...It's a bad coincidence that your type is called "Metadata", the ToolingGroup.MetaData class is the metadata that RIA services uses.