实体框架/RIA 服务包括不工作 [英] Entity Framework / RIA Services Include not working

查看:18
本文介绍了实体框架/RIA 服务包括不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SL4/WCF RIA Services/EF 4 应用程序.我无法将我的包含实体放入我的 SL4 数据上下文中.

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
    }

我将它分配给 var 组,以便在方法返回之前对其进行检查.如果我在方法返回之前设置一个断点并向我的 Watch 窗口添加一行,MetaData 就在那里:

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

当我让方法返回并在 Silverlight ui 完成事件中检查它时,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
    }

当我在监视窗口中执行此操作时,MetaData 为空:

When I do this in my watch window MetaData is null:

_toolingContext.ToolingGroups.First().MetaData

我检查以确保在两种情况下调用 .First() 返回的 ToolingGroup 是同一个实体,而且确实如此.

I checked to make sure the ToolingGroup returned by the call to .First() in both cases was the same entity and it was.

为什么服务方法和我的 ui 方法之间的 MetaData 会丢失(例如 null)?

Why is MetaData lost (eg. null) between the service method and my ui method?

解决方案:

// 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] 属性.像这样...

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...

[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 服务使用的元数据.

It's a bad coincidence that your type is called "Metadata", the ToolingGroup.MetaData class is the metadata that RIA services uses.

这篇关于实体框架/RIA 服务包括不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆