2010-06-23 79 views
1

我想使用sql到linq從storedProc獲取多個結果集。我無法從設計器生成它,所以我在designer.cs文件中寫下了代碼。但是每當我給設計器添加一些東西時,它會用.dbml文件中的標記刷新設計器,因此每次添加內容時都會刪除下面的代碼。我必須每次複製它。如果我能爲此獲得相應的dbml標記,那就太好了。StoredProcedure返回多個結果集Sql使用設計器Linq使用設計器

[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

回答

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. 獲取模塊類型和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 
        }; 

UPDATE
還有一個更好的方法。 右鍵單擊解決方案資源管理器中的dbml文件,然後選擇打開。選擇XML Editor,當您在Visual Studio中保存文件時,它會自動生成designer.cs文件。

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)); 
    } 
} 
+0

整個designer.cs文件丟失了。不知道在哪裏,而不是它,我得到MyDataContext.cs文件爲空。 :( – IsmailS 2010-06-23 08:34:48

+0

我不得不從Visual Studio外部添加文件,然後將其包含在項目中。 – IsmailS 2010-06-23 08:50:01

+0

當我在設計器上更改某些內容(添加表格)時,它仍然會移除designer.cs文件 – IsmailS 2010-06-23 09:03:20