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

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

问题描述

我有一个SL4 / WCF RIA Services / EF 4应用程序。我无法将包含的实体纳入我的SL4数据环境。



在应用程序的服务器端服务部分,这是我的方法:

  [Query(IsDefault = true)] 
public IQueryable< ToolingGroup> GetToolingGroups()
{
var groups = this.ObjectContext.ToolingGroups.Include(MetaData)。OrderBy(g => g.Name);
返回组; //断点设置在这里
}

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

  groups.First ).MetaData 

当我让方法返回并在silverlight中检查它完成的事件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)?



解决方案:

  // MetadataTypeAttribute将ToolingGroupMetadata标识为class 
//,它携带ToolingGroup类的附加元数据。
[MetadataTypeAttribute(typeof(ToolingGroup.ToolingGroupMetadata))]
public partial class ToolingGroup
{

//此类允许您将自定义属性附加到属性
// ToolingGroup类。
//
//例如,以下将Xyz属性标记为
//必需属性,并指定有效值的格式:
// [必需]
// [RegularExpression([AZ] [A-Za-z0-9] *)]
// [StringLength(32)]
// public string Xyz {get;组; }
内部密封类ToolingGroupMetadata
{

//元数据类不是要实例化的。
private ToolingGroupMetadata()
{
}

public int Id {get;组; }

[Include] //已添加MetaData序列化
public MetaData MetaData {get;组; }

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

public string Name {get;组; }

public ToolingCategory ToolingCategory {get;组; }

public int ToolingCategoryId {get;组; }

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


解决方案

这里有两层,EF和RIA服务。你已经处理了EF部分。现在,您需要告诉RIA服务,以便在通过电缆序列化您的实体时包括该属性。在实体的元数据中,添加 [Include] 属性。像这样...

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

//添加此属性告诉RIA服务
//也可以将此属性跨
[Include]
public MetaData MetaData {get ; set;}
}
}

你的类型是一个不巧的巧合被称为元数据,ToolingGroup.MetaData类是RIA服务使用的元数据。


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

解决方案

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

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天全站免登陆