基于POCO实体的RIA服务无法反序列化关联实体 [英] POCO entity-based RIA service can't de-serialize associated entities

查看:144
本文介绍了基于POCO实体的RIA服务无法反序列化关联实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RIA服务开发Silverlight业务应用程序,该服务将返回POCO实体(TaskTime和User). TaskTime实体具有与之关联的User实体.

I'm developing a Silverlight Business Application, using a RIA service, which is returning POCO entities (TaskTime and User). The TaskTime entity has a User entity associated with it.

我创建了一个域服务,该域服务具有一个查询函数(GetTimesheet),该函数返回TaskTime实体的IQueryable集合,如果我不尝试获取关联的User实体的话,也可以正常工作,但是只要包含在[TaskTime]实体中[User]属性上方的[Include]和[Association]属性,我开始收到反序列化错误,说:

I have created a domain service which has a query function (GetTimesheet) which returns an IQueryable collection of TaskTime entities, which works fine if I don't try and get the associated User entities as well, but as soon as I include the [Include] and [Association] attributes above the 'User' property in the 'TaskTime' entity I start getting deserialization errors saying:

格式化程序在尝试反序列化消息时引发异常[...] InnerException消息为第1行位置266错误.元素'http://schemas.microsoft.com/2003/10/Serilaization/Arrays:anyType'包含'http://schemas.datacontract.org/2004/07/Timesheets.Entities:User'数据合同的数据.解串器不知道任何映射到该合同的类型.将相应的用户"添加到已知类型的列表中..."

这表明我使用了'KnownTypes'属性,但似乎找不到解决该错误的地方.

It suggests that I use the 'KnownTypes' attribute, but I can't seem to find a place to put that which resolves this error.

有人知道如何解决此问题吗?我可以在Silverlight应用程序的"Generated_Code"中看到这两种类型似乎都已正确创建,并添加了DataContract属性等.

Does anyone have any clue how to resolve this? I can see in the 'Generated_Code' in my Silverlight application that both types seem to be created properly, with DataContract attributes added etc..

我的POCO实体的简化版本为:

Simplified versions of my POCO entities are:

public partial class TaskTime
{
    [Key()]
    public virtual int ID   { get; set; }

    public virtual int User_ID  { get; set; }

    [Include]
    [Association("TaskTime_User", "User_ID", "ID", IsForeignKey=true)]
    public virtual User User
    {
        get { return _user; }
        set
        {
            if (!ReferenceEquals(_user, value))
            {
                var previousValue = _user;
                _user = value;
                FixupUser(previousValue);
            }
        }
    }
}

public partial class User
{
    [Key()]
    public virtual int ID   { get; set; }

    public virtual string Name  { get; set; }
}

推荐答案

这可能是因为您的DomainService中没有用于User类的CRUD操作的方法(我猜是因为您没有提供用于您的服务).

This is probably because you don't have methods in your DomainService for CRUD operations on the User class (I am guessing since you haven't supplied the code for your service).

在RIA中,如果要向客户端公开类型,则必须执行以下两项操作之一:

In RIA, if you want to expose a type to the client, you have to do one of two things:

(A)在服务上公开该类型的CRUD操作

(A) Expose CRUD operations for that type on the service

-或-

(B)在父类(在本例中为TaskTime类)上使用[Composition]属性.

(B) Use the [Composition] attribute on the parent class (in this case, your TaskTime class).

[Composition]属性使得RIA仅通过其父级将CRUD操作授予User类-因此User类在Service上将没有其自己的CRUD操作,因此只能通过其父级进行更新班级.

The [Composition] attribute makes it so that RIA will only grant CRUD operations to the User class via it's parent - so the User class won't have it's own CRUD operations on the Service and can thus only be updated through its parent class.

您选择哪种方式取决于您希望应用程序运行的方式.在某些情况下,[组成]是合适的.

Which road you choose depends on how you want your app to function. In some cases, [Composition] is appropriate.

这篇关于基于POCO实体的RIA服务无法反序列化关联实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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