S#harp 架构映射多对多和 ado.net 数据服务:预期结果为单个资源,但找到了多个资源

发布于 2024-08-13 17:09:54 字数 3407 浏览 6 评论 0原文

我正在开发一个应用程序,该应用程序通过 ADO.NET 数据服务从具有 nHibernate 和 s#arp 架构的 SQL Server 数据库(从旧数据库迁移)读取数据。我正在尝试映射多对多关系。我有一个错误类:

public class Error
{
    public virtual int ERROR_ID { get; set; }
    public virtual string ERROR_CODE { get; set; }
    public virtual string DESCRIPTION { get; set; }

    public virtual IList<ErrorGroup> GROUPS { get; protected set; }
}

然后我有错误组类:

public class ErrorGroup
{
    public virtual int ERROR_GROUP_ID {get; set;}
    public virtual string ERROR_GROUP_NAME { get; set; }
    public virtual string DESCRIPTION { get; set; }

    public virtual IList<Error> ERRORS { get; protected set; }
}

和覆盖:

public class ErrorGroupOverride : IAutoMappingOverride<ErrorGroup>
{
    public void Override(AutoMapping<ErrorGroup> mapping)
    {
        mapping.Table("ERROR_GROUP");
        mapping.Id(x => x.ERROR_GROUP_ID, "ERROR_GROUP_ID");
        mapping.IgnoreProperty(x => x.Id);
        mapping.HasManyToMany<Error>(x => x.Error)
            .Table("ERROR_GROUP_LINK")
            .ParentKeyColumn("ERROR_GROUP_ID")
            .ChildKeyColumn("ERROR_ID").Inverse().AsBag();
    }
}

public class ErrorOverride : IAutoMappingOverride<Error>
{
    public void Override(AutoMapping<Error> mapping)
    {
        mapping.Table("ERROR");
        mapping.Id(x => x.ERROR_ID, "ERROR_ID");
        mapping.IgnoreProperty(x => x.Id);
        mapping.HasManyToMany<ErrorGroup>(x => x.GROUPS)
            .Table("ERROR_GROUP_LINK")
            .ParentKeyColumn("ERROR_ID")
            .ChildKeyColumn("ERROR_GROUP_ID").AsBag();
    }
}

当我在浏览器中查看数据服务时,例如: http://localhost:1905/DataService.svc/Errors它显示了没有问题的错误列表,并且像 http://localhost:1905/DataService.svc/Errors(123) 一样使用它也可以。

问题 当我想查看组中的错误或组形成错误时,例如:“http ://localhost:1905/DataService.svc/Errors(123)?$expand=GROUPS" 我得到了 XML 文档,但浏览器说:

The XML page cannot be displayed 
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. 
--------------------------------------------------------------------------------
Only one top level element is allowed in an XML document. Error processing resource 'http://localhost:1905/DataServic...
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
-^

我查看了源代码,并且我得到了数据< /强>。然而它有一个例外:

<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  <code></code>
  <message xml:lang="en-US">An error occurred while processing this request.</message>
  <innererror xmlns="xmlns">
    <message>A single resource was expected for the result, but multiple resources were found.</message>

    <type>System.InvalidOperationException</type>
    <stacktrace>   at System.Data.Services.Serializers.Serializer.WriteRequest(IEnumerator queryResults, Boolean hasMoved)&#xD;
   at System.Data.Services.ResponseBodyWriter.Write(Stream stream)</stacktrace>
  </innererror>
</error>

AI 缺少某些东西???这个错误从何而来?

I'm developing an application that reads data from a SQL server database (migrated from a legacy DB) with nHibernate and s#arp architecture through ADO.NET Data services. I'm trying to map a many-to-many relationship. I have a Error class:

public class Error
{
    public virtual int ERROR_ID { get; set; }
    public virtual string ERROR_CODE { get; set; }
    public virtual string DESCRIPTION { get; set; }

    public virtual IList<ErrorGroup> GROUPS { get; protected set; }
}

And then I have the error group class:

public class ErrorGroup
{
    public virtual int ERROR_GROUP_ID {get; set;}
    public virtual string ERROR_GROUP_NAME { get; set; }
    public virtual string DESCRIPTION { get; set; }

    public virtual IList<Error> ERRORS { get; protected set; }
}

And the overrides:

public class ErrorGroupOverride : IAutoMappingOverride<ErrorGroup>
{
    public void Override(AutoMapping<ErrorGroup> mapping)
    {
        mapping.Table("ERROR_GROUP");
        mapping.Id(x => x.ERROR_GROUP_ID, "ERROR_GROUP_ID");
        mapping.IgnoreProperty(x => x.Id);
        mapping.HasManyToMany<Error>(x => x.Error)
            .Table("ERROR_GROUP_LINK")
            .ParentKeyColumn("ERROR_GROUP_ID")
            .ChildKeyColumn("ERROR_ID").Inverse().AsBag();
    }
}

public class ErrorOverride : IAutoMappingOverride<Error>
{
    public void Override(AutoMapping<Error> mapping)
    {
        mapping.Table("ERROR");
        mapping.Id(x => x.ERROR_ID, "ERROR_ID");
        mapping.IgnoreProperty(x => x.Id);
        mapping.HasManyToMany<ErrorGroup>(x => x.GROUPS)
            .Table("ERROR_GROUP_LINK")
            .ParentKeyColumn("ERROR_ID")
            .ChildKeyColumn("ERROR_GROUP_ID").AsBag();
    }
}

When I view the Data service in the browser like: http://localhost:1905/DataService.svc/Errors it shows the list of errors with no problems, and using it like http://localhost:1905/DataService.svc/Errors(123) works too.

The Problem
When I want to see the Errors in a group or the groups form an error, like: "http://localhost:1905/DataService.svc/Errors(123)?$expand=GROUPS" I get the XML Document, but the browser says:

The XML page cannot be displayed 
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. 
--------------------------------------------------------------------------------
Only one top level element is allowed in an XML document. Error processing resource 'http://localhost:1905/DataServic...
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
-^

I view the sourcecode, and I get the data. However it comes with an exception:

<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  <code></code>
  <message xml:lang="en-US">An error occurred while processing this request.</message>
  <innererror xmlns="xmlns">
    <message>A single resource was expected for the result, but multiple resources were found.</message>

    <type>System.InvalidOperationException</type>
    <stacktrace>   at System.Data.Services.Serializers.Serializer.WriteRequest(IEnumerator queryResults, Boolean hasMoved)
   at System.Data.Services.ResponseBodyWriter.Write(Stream stream)</stacktrace>
  </innererror>
</error>

A I missing something??? Where does this error come from?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吲‖鸣 2024-08-20 17:09:54

嗯...不熟悉这个错误,但我想知道它是否与延迟加载有关。您当前的映射将导致子集合在被访问时被查询,这可能会导致序列化崩溃。序列化 NHibernate 代理对象是一个充满巫术的巨大痛苦。尝试像下面的示例一样更改您的映射,看看它是否允许您继续。

public class ErrorOverride : IAutoMappingOverride<Error>
{
    public void Override(AutoMapping<Error> mapping)
    {
        mapping.Table("ERROR");
        mapping.Id(x => x.ERROR_ID, "ERROR_ID");
        mapping.IgnoreProperty(x => x.Id);
        mapping.HasManyToMany<ErrorGroup>(x => x.GROUPS)
            .Table("ERROR_GROUP_LINK")
            .ParentKeyColumn("ERROR_ID")
            .ChildKeyColumn("ERROR_GROUP_ID").Not.Lazy().AsBag();
    }
}

hrm... not familiar with this error but I wonder if it has something to do with lazy loading. Your current mapping would cause the child collection to be queried for when it is accessed which could be causing the serialization to freak out. Serializing the NHibernate proxied objects is a massive pain in the ass full of voodoo. Try changing your mapping like the example below and see if it allows you to continue.

public class ErrorOverride : IAutoMappingOverride<Error>
{
    public void Override(AutoMapping<Error> mapping)
    {
        mapping.Table("ERROR");
        mapping.Id(x => x.ERROR_ID, "ERROR_ID");
        mapping.IgnoreProperty(x => x.Id);
        mapping.HasManyToMany<ErrorGroup>(x => x.GROUPS)
            .Table("ERROR_GROUP_LINK")
            .ParentKeyColumn("ERROR_ID")
            .ChildKeyColumn("ERROR_GROUP_ID").Not.Lazy().AsBag();
    }
}
ペ泪落弦音 2024-08-20 17:09:54

我认为这是由于 linq 和 nHibernate 的错误造成的,在我将结果限制为仅返回一个之后才起作用。

I think this was due to an error with linq and nHibernate, worked after I restricted the results to return just one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文