简体   繁体   English

实体框架/ RIA服务包括不工作

[英]Entity Framework / RIA Services Include not working

I've got a SL4 / WCF RIA Services / EF 4 application. 我有一个SL4 / WCF RIA服务/ EF 4应用程序。 I'm having trouble getting my Included entity into my SL4 data context. 我无法将包含的实体放入SL4数据上下文中。

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. 我将它分配给var组以允许在方法返回之前检查它。 If I set a breakpoint before the method returns and add a line to my Watch window the MetaData is there: 如果我在方法返回之前设置断点并在Watch窗口中添加一行,那么MetaData就在那里:

groups.First().MetaData

When I let the method return and check it in the silverlight ui completed event MetaData is null. 当我让方法返回并在silverlight ui完成事件中检查它时MetaData为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: 当我在监视窗口中执行此操作时,MetaData为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. 我检查过以确保在两种情况下通过调用.First()返回的ToolingGroup都是同一个实体。

Why is MetaData lost (eg. null) between the service method and my ui method? 为什么MetaData在服务方法和我的ui方法之间丢失(例如null)?

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; }
    }
}

There are two layers at play here, EF and RIA Services. 这里有两层,EF和RIA服务。 You've handled the EF part. 你已经处理了EF部分。 Now you need to tell RIA services to include that property when it serializes your entities across the wire. 现在,您需要告诉RIA服务在通过线路序列化实体时包含该属性。 In your metadata for the entity, add the [Include] attribute. 在实体的元数据中,添加[Include]属性。 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; }
    }
}

It's a bad coincidence that your type is called "Metadata", the ToolingGroup.MetaData class is the metadata that RIA services uses. 您的类型称为“元数据”,巧妙的巧合,ToolingGroup.MetaData类是RIA服务使用的元数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有继承和RIA服务的实体框架模型 - Entity Framework Model with inheritance and RIA Services 实体框架数据库优先和RIA服务导航属性 - Entity Framework database first and RIA services navigation properties 我可以在实体框架4.3中使用RIA服务吗? - Can I use RIA Services with Entity Framework 4.3? 实体框架与WCF RIA服务的紧密程度如何? - How closely is Entity Framework tied to WCF RIA Services? 仅使用实体框架和RIA服务更新已修改的列 - Update modified columns only, with Entity Framework and RIA services 带有RIA服务的实体框架,Silverlight - 脱钩与快速开发的权衡 - Entity Framework with RIA services, Silverlight - tradeoff of decoupling versus rapid development Ria Services:使用两个数据模型 - Ria Services:working with two DataModels 如何编辑绑定到ria服务然后绑定到Entity Framework和SQL递归表的Silverlight树视图数据 - How to Edit a Silverlight treeview data bound to ria services then to Entity Framework and SQL recursive table 将数据从WCF Ria Services绑定到数据网格Entity Framework Code First - Bind data from WCF Ria Services to data grid Entity Framework Code First 需要帮助调试:无法通过RIA服务,实体框架,MySQL将数据获取到Silverlight应用程序 - Need help debugging: Having trouble getting data to Silverlight App through RIA Services, Entity Framework, MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM