Pergunta

I want to get multiple resultsets from a storedProc using sql to linq. I was not able to generate it from designer so I wrote below code in designer.cs file. But whenever I add something to designer, it refreshes the designer with the markup in .dbml file and hence it removes the below code every time I add something. I have to copy it every time. If I can get corresponding dbml markup for this, it would be great.

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

I've already defined Module and ModuleOption as tables. Now when I add below markup in .dbml file it complains 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>

I'm using Visual Studio 2008 SP1

Foi útil?

Solução 2

I'm replying my own answer.

One cannot use the already defined type for result set of stored procedure. So I had to change the name of ElementType to ModuleResult and 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>

Here are the steps which I took to solve the above problem.

  1. Delete the .designer.cs file
  2. Add above markup to the .dbml file
  3. Exclude .dbml and .dbml.layout file
  4. Include .dbml and .dbml.layout file (this will generate .designer.cs file again but will not include it in the project).
  5. Include .designer file in project.
  6. Get the list of Module type and ModuleOption type as below.


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

UPDATE
Still a better way. Right click the dbml file in solution explorer and select open with. Choose XML Editor and when you save the file within visual studio it automatically generates the designer.cs file.

Outras dicas

Add the method to the partial class for the data context.

You achieve this by adding a file with the same name as the data context alongside the dbml file, and use the class declaration:

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));
    }   
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top