StoredProcess使用designer将多个resultset Sql返回给Linq
我想使用sql to linq从storedProc获取多个结果集。我无法从designer生成它,所以我在designer.cs文件中编写了以下代码。但是,每当我向设计器添加一些内容时,它都会用.dbml文件中的标记刷新设计器,因此每次添加内容时它都会删除下面的代码。我每次都得抄。如果我能为此获得相应的dbml标记,那就太好了StoredProcess使用designer将多个resultset Sql返回给Linq,sql,linq-to-sql,stored-procedures,multiple-results,Sql,Linq To Sql,Stored Procedures,Multiple Results,我想使用sql to linq从storedProc获取多个结果集。我无法从designer生成它,所以我在designer.cs文件中编写了以下代码。但是,每当我向设计器添加一些内容时,它都会用.dbml文件中的标记刷新设计器,因此每次添加内容时它都会删除下面的代码。我每次都得抄。如果我能为此获得相应的dbml标记,那就太好了 [Function(Name = "dbo.GetAllModulesAndOptions")] [ResultType(typeof(Module))] [Resul
[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:类型元素的名称属性“Module”已被其他类型使用
我正在使用Visual Studio 2008 SP1将该方法添加到数据上下文的分部类中 通过在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));
}
}
我在回答我自己的问题。 不能将已定义的类型用于存储过程的结果集。因此,我必须将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>
以下是我为解决上述问题所采取的步骤
删除.designer.cs文件
将上述标记添加到.dbml文件中
排除.dbml和.dbml.layout文件
Include.dbml和.dbml.layout文件这将再次生成.designer.cs文件,但不会将其包含在项目中。
在项目中包含.designer文件。
获取模块类型和模块操作类型的列表,如下所示。
更新
还是更好的方法。
在解决方案资源管理器中右键单击dbml文件,然后选择“打开方式”。选择XML编辑器,在visual studio中保存文件时,它会自动生成designer.cs文件。整个designer.cs文件丢失。不知道在哪里,取而代之的是MyDataContext.cs文件为空:我必须从visual studio外部添加该文件,然后将其包含在项目中。当我更改某些内容时,它仍然会删除designer.cs文件,并在设计器上添加表
<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>
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
};