一个存储过程returns多个结果集时dbml应该怎么写?

How dbml should be written when a stored procedure returns multiple result sets?

有没有办法也修改 dbml,使 designer.cs 中的方法保持为 IMultipleResults?

我有一个存储过程returns 2 组数据。 当我将程序拖放到 dbml 时,dbml

生成 xml 和 designer.cs
<Function Name="dbo.MultiResultsTest" Method="MultiResultsTest">
<Parameter Name="iCustomerID" Type="System.Int32" DbType="Int" />
<Parameter Name="iProductID" Type="System.Int32" DbType="Int" />
<ElementType Name="MultiResultsTestResult">
  <Column Name="CustomerID" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
  <Column Name="Name" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
  <Column Name="Phone" Type="System.String" DbType="VarChar(14)" CanBeNull="true" />
  <Column Name="Email" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
  <Column Name="Address" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
</ElementType>

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.MultiResultsTest")]
public ISingleResult<MultiResultsTestResult> MultiResultsTest([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> iCustomerID, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> iProductID)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), iCustomerID, iProductID);
    return ((ISingleResult<MultiResultsTestResult>)(result.ReturnValue));
}

为了获得多个结果,我将 designer.cs 修改为如下所示:

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.MultiResultsTest")]
[ResultType(typeof(Customer))]
[ResultType(typeof(Product))]
public IMultipleResults MultiResultsTest([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> iCustomerID, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> iProductID)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), iCustomerID, iProductID);
    return (IMultipleResults)(result.ReturnValue);
}

这很好用,但是因为 dbml,当我向它添加更多内容并保存 designer.cs 中的方法时,会恢复到 ISingleResult 方法。

经过一些搜索和尝试,我发现这种方法很有效!

<Function Name="dbo.MultiResultsTest" Method="MultiResultsTest">
<Parameter Name="iCustomerID" Type="System.Int32" DbType="Int" />
<Parameter Name="iProductID" Type="System.Int32" DbType="Int" />
<ElementType Name="CustomerResult">
  <Column Name="CustomerID" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
  <Column Name="Name" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
  <Column Name="Phone" Type="System.String" DbType="VarChar(14)" CanBeNull="true" />
  <Column Name="Email" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
  <Column Name="Address" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
</ElementType>
<ElementType Name="ProductResult">
  <Column Name="ProductID" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
  <Column Name="ProductName" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
  <Column Name="Price" Type="System.Decimal" DbType="Decimal(18,2)" CanBeNull="true" />
  <Column Name="ModelNumber" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
</ElementType>

这会在 designer.cs

中生成以下方法
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.MultiResultsTest")]
[global::System.Data.Linq.Mapping.ResultTypeAttribute(typeof(CustomerResult))]
[global::System.Data.Linq.Mapping.ResultTypeAttribute(typeof(ProductResult))]
public IMultipleResults MultiResultsTest([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> iCustomerID, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> iProductID)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), iCustomerID, iProductID);
    return ((IMultipleResults)(result.ReturnValue));
}