使用Designer返回多个结果集SQL将多个结果集SQL返回LINQ
-
29-09-2019 - |
题
我想使用SQL到LINQ从StoredProc中获取多个结果集。我无法从设计师中生成它,因此我在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));
}
我已经将模块和模块定义为表。现在,当我在.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
解决方案 2
我要回答自己的答案。
一个人不能将已定义的类型用于存储过程的结果集。因此,我不得不将ElementType的名称更改为模块和模块。
<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文件
- 包含.dbml和.dbml.layout文件(这将再次生成.designer.cs文件,但不会将其包含在项目中)。
- 在项目中包含.Designer文件。
- 如下获取模块类型和模块类型的列表。
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文件。
其他提示
将方法添加到数据上下文的部分类中。
您可以通过添加与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));
}
}
不隶属于 StackOverflow