LINQ to SQL - 嵌套存储过程

时间:2012-03-05 21:42:23

标签: linq linq-to-sql stored-procedures data-modeling

我有两个存储过程。一个(test_proc_outside)调用第二个(test_proc_inside)。

当我通过拖动我的test_proc_outside存储过程直观地创建一个新的LINQ to SQL .dbml文件时,生成的类(test_proc_outsideResult)实际上包含内部过程(test_proc_inside)结果集的建模。

以下是外部存储过程的代码(test_proc_outside):

CREATE PROCEDURE [dbo].[test_proc_outside] 
@test1 int = 1, 
@test2 int = 2,
@test3 int = 3

AS 开始      - 添加SET NOCOUNT ON以防止出现额外的结果集      - 干扰SELECT语句。     SET NOCOUNT ON;

-- The result of this proc call gets modelled into DBML:
EXEC dbo.test_proc_inside @test1, @test2

-- This SELECT statement does NOT get modelled into DBML:
SELECT  @test1 AS Test1,
        @test2 AS Test2,
        @test3 AS Test3

END GO

以下是内部存储过程的代码(test_proc_inside):

CREATE PROCEDURE [dbo].[test_proc_inside] 
-- Add the parameters for the stored procedure here
@test1 int = 1, 
@test2 int = 2

AS 开始      - 添加SET NOCOUNT ON以防止出现额外的结果集      - 干扰SELECT语句。     SET NOCOUNT ON;

-- This is the result set that gets modelled by the DBML file:
SELECT  @test1 AS Test1_Inside,
        @test2 AS Test2_Inside

END GO

DBML生成似乎在存储过程(或嵌套存储过程)中查找第一个结果集,并为此喷出模型。

如果我将嵌套的proc调用更改为函数,我会得到我想要的模型(test_proc_outside)。

是否有配置设置告诉DBML文件在外部proc(test_proc_outside)上生成结果集的类,而不是打扰test_proc_inside的内部结果?

提前致谢,

克雷格

1 个答案:

答案 0 :(得分:0)

我认为用L2S获得你想要的结果是可能的,但你也必须得到所有其他结果。您无法通过设计器执行此操作,您必须通过使用返回IMultipleResults接口实例的方法在部分类中扩展DataContext来映射sproc。请参阅此博客文章:

http://blogs.msdn.com/b/dinesh.kulkarni/archive/2008/05/16/linq-to-sql-tips-7.aspx

此外,如果存储过程返回的结果集不会映射到L2S模型中的实体,那么您需要创建特殊类型以通过在代码中以与L2S相同的方式定义这些结果来手动表示这些结果通常在designer.cs中为存储过程结果生成类型。

相关问题