1

我想使用 sql to linq 从存储过程中获取多个结果集。我无法从设计器生成它,所以我在 Designer.cs 文件中编写了以下代码。但是每当我向设计器添加一些东西时,它都会使用 .dbml 文件中的标记刷新设计器,因此每次我添加一些东西时它都会删除下面的代码。我必须每次都复制它。如果我能为此获得相应的 dbml 标记,那就太好了。

[Function(Name = "dbo.GetAllModulesAndOptions")]
[ResultType(typeof(Module))]
[ResultType(typeof(ModuleOption))]
public IMultipleResults GetAllModules()
{
  IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
  return ((IMultipleResults)(result.ReturnValue));
}

我已经将 Module 和 ModuleOption 定义为表。现在,当我在 .dbml 文件中添加以下标记时,它会抱怨 DBML1114: The Name attribute 'Module' of the Type element is already used by another type.

  <Function Name="dbo.GetAllModulesAndOptions" Method="GetAllModules">
    <ElementType Name="Module">
      <Column Name="ModuleId" Type="System.Int64" DbType="BigInt NOT NULL" CanBeNull="false" />
      <Column Name="ModuleName" Type="System.String" DbType="VarChar(50)" CanBeNull="true" />
      <Column Name="Description" Type="System.String" DbType="VarChar(255)" CanBeNull="true" />
      <Column Name="SalesDesc" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
      <Column Name="ParentModuleId" Type="System.Int32" DbType="Int" CanBeNull="true" />
    </ElementType>
    <ElementType Name="ModuleOption">
      <Column Name="ModuleOptionId" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
      <Column Name="ModuleOptionName" Type="System.String" DbType="VarChar(50)" CanBeNull="true" />
      <Column Name="ModuleOptionDesc" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
      <Column Name="DefaultPrice" Type="System.Decimal" DbType="Money" CanBeNull="true" />
      <Column Name="ModuleId" Type="System.Int64" DbType="BigInt" CanBeNull="true" />
      <Column Name="InUse" Type="System.Int32" DbType="Int" CanBeNull="true" />
    </ElementType>
  </Function>

我正在使用 Visual Studio 2008 SP1

4

2 回答 2

0

将该方法添加到数据上下文的分部类中。

您可以通过在 dbml 文件旁边添加一个与数据上下文同名的文件来实现此目的,并使用类声明:

public partial class YourDataContext
{
    [Function(Name = "dbo.GetAllModulesAndOptions")]
    [ResultType(typeof(Module))]
    [ResultType(typeof(ModuleOption))]
    public IMultipleResults GetAllModules()
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo (MethodInfo.GetCurrentMethod())));
        return ((IMultipleResults)(result.ReturnValue));
    }   
}
于 2010-06-23T08:30:55.077 回答
0

我正在回复我自己的答案。

不能将已经定义的类型用于存储过程的结果集。所以我不得不把ElementType的名字改成ModuleResult和ModuleOptionResult。

  <Function Name="dbo.GetAllModulesAndOptions" Method="GetAllModules">
    <ElementType Name="ModuleResult">
      <Column Name="ModuleId" Type="System.Int64" DbType="BigInt NOT NULL" CanBeNull="false" />
      <Column Name="ModuleName" Type="System.String" DbType="VarChar(50)" CanBeNull="true" />
      <Column Name="Description" Type="System.String" DbType="VarChar(255)" CanBeNull="true" />
      <Column Name="SalesDesc" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
      <Column Name="ParentModuleId" Type="System.Int32" DbType="Int" CanBeNull="true" />
    </ElementType>
    <ElementType Name="ModuleOptionResult">
      <Column Name="ModuleOptionId" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
      <Column Name="ModuleOptionName" Type="System.String" DbType="VarChar(50)" CanBeNull="true" />
      <Column Name="ModuleOptionDesc" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
      <Column Name="DefaultPrice" Type="System.Decimal" DbType="Money" CanBeNull="true" />
      <Column Name="ModuleId" Type="System.Int64" DbType="BigInt" CanBeNull="true" />
      <Column Name="InUse" Type="System.Int32" DbType="Int" CanBeNull="true" />
    </ElementType>
  </Function>

这是我为解决上述问题而采取的步骤。

  1. 删除 .designer.cs 文件
  2. 将上述标记添加到 .dbml 文件
  3. 排除 .dbml 和 .dbml.layout 文件
  4. 包含 .dbml 和 .dbml.layout 文件(这将再次生成 .designer.cs 文件,但不会将其包含在项目中)。
  5. 在项目中包含 .designer 文件。
  6. 获取 Module 类型和 ModuleOption 类型的列表,如下所示。


var modules = from row in results.GetResult<ModuleResult>().ToList()
                       select new Module
                                {
                                  ModuleId = row.ModuleId,
                                  ModuleName = row.ModuleName,
                                  Description = row.Description,
                                  SalesDesc = row.SalesDesc,
                                  ParentModuleId = row.ParentModuleId
                                };

var moduleOptions = from row in results.GetResult<ModuleOptionResult>().ToList()
                    select new ModuleOption
                    {
                      ModuleOptionId = row.ModuleOptionId,
                      ModuleOptionName = row.ModuleOptionName,
                      ModuleOptionDesc = row.ModuleOptionDesc,
                      DefaultPrice = row.DefaultPrice,
                      ModuleId = row.ModuleId,
                      InUse = row.InUse
                    };

更新
仍然是一个更好的方法。 在解决方案资源管理器中右键单击 dbml 文件并选择打开方式。选择XML Editor并在 Visual Studio 中保存文件时,它会自动生成 Designer.cs 文件。

于 2010-06-24T03:30:47.667 回答