简体   繁体   English

关于linq2sql的问题

[英]Question about linq2sql

I have some legacy code with linq2sql classes. 我有一些linq2sql类的旧代码。 DataContext class has following definition for stored procedure: DataContext类对存储过程具有以下定义:

 [Function(Name="dbo.sp_Goods_SelectBySection")]
        public ISingleResult<sp_Goods_SelectBySectionResult> sp_Goods_SelectBySection([Parameter(Name="SectionId", DbType="Int")] System.Nullable<int> sectionId)
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), sectionId);
            return ((ISingleResult<sp_Goods_SelectBySectionResult>)(result.ReturnValue));
        }

But when i am trying create same procedure in my own project, designer creating procedure with different implementation: 但是,当我尝试在自己的项目中创建相同的过程时,设计师将创建具有不同实现的过程:

[Function(Name="dbo.sp_Goods_SelectBySection")]
        public int sp_Goods_SelectBySection([Parameter(Name="SectionId", DbType="Int")] System.Nullable<int> sectionId)
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), sectionId);
            return ((int)(result.ReturnValue));
        }

And i cannt change type in method properties. 而且我无法更改方法属性的类型。

Here is code of stored procedure. 这是存储过程的代码。

ALTER PROCEDURE [dbo].[sp_Goods_SelectBySection]
(
    @SectionId INT
)
AS
BEGIN

    SET NOCOUNT ON;

    SELECT 
        Goods.Id,
        Goods.[Name],
        TypeId,
        Makerid,
        Price,
        [Description],
        Term,
        Types.Name AS TypeName,
        Makers.Name AS MakerName,
        GoodsImage.SmallImageUrl AS MainImageUrl,
        Goods.IsDeleted,
        Goods.Rang
    FROM Goods 
         INNER JOIN Types ON (Types.Id = Goods.TypeId)
         INNER JOIN Makers ON (Makers.Id = Goods.MakerId)
         LEFT JOIN GoodsImage ON ( GoodsImage.GoodId = Goods.Id AND GoodsImage.IsMain = 1 AND GoodsImage.IsDeleted = 0 )
    WHERE 
        Types.SectionId = @SectionId
        AND Goods.IsDeleted  = 0
    ORDER BY Rang ASC       
END

Why? 为什么? And how i can create procedure which returning ISingleResult instead of int? 以及如何创建返回ISingleResult而不是int的过程?

Note i have many such procedures and modify by hand each of them is not good idea i think. 注意我有很多这样的程序,并且手工修改它们并不是我认为的好主意。


I guess it's designer problem. 我想这是设计师的问题。 Because i created new test project and it work ok. 因为我创建了新的测试项目,所以可以正常工作。 Thanks to all who helped me. 感谢所有帮助我的人。 Especially to Andras Zoltan. 特别是对Andras Zoltan。

Sorry, slightly misunderstood your question but I'll keep my initial comments 抱歉,您的问题有点误解了,但我会保留我的最初评论

Remember that stored procedures return a result set , not a value; 请记住,存储过程返回的是结果集 ,而不是值。 unless you count the value returned in a return statement. 除非您计算在return语句中返回的值。 Therefore you need a type to wrap around the columns and rows that are returned. 因此,您需要一种类型来包装返回的列和行。

You can change the return type to a table if you know the SP returns a rowset that maps to one of your table types. 如果您知道SP返回的行集映射到您的一种表类型,则可以将返回类型更改为表。 But you are not going to be able to just return 'an int'. 但是您将不能只返回“ int”。

See this link: http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/1667a989-08a0-402c-9358-e4637406a75f (sorry it's over on MSDN forums - not SO :$) and the linked topic from that http://msdn.microsoft.com/en-us/library/bb386975.aspx 请参阅此链接: http : //social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/1667a989-08a0-402c-9358-e4637406a75f (对不起,它已经在MSDN论坛上结束-并非SO:$),并且该链接的主题http://msdn.microsoft.com/zh-cn/library/bb386975.aspx

Update 更新资料

L2S uses the equivalent of SQL reflection to figure what an SP returns in order to auto-generate the result. L2S使用等效的SQL反射来计算SP返回的内容,以便自动生成结果。

If the SP doesn't SELECT , or if it only has a RETURN in it, you will get a basic int return type for your mapped function. 如果SP不是SELECT ,或者仅包含RETURN ,则将为映射函数获得基本的int返回类型。 This kind of thing can happen legitimately or erroneously if the SP uses IF statements to fork and return different results accordingly; 如果SP使用IF语句分叉并相应地返回不同的结果,则这种事情可能会合法或错误地发生; L2S cannot possibly figure out a single return type that can satisfy all possible outcomes, and so it just plumps for one of them. L2S不可能找出可以满足所有可能结果的单一收益类型,因此只能满足其中之一。

As @JohnOpincar mentions in his comments, there is a question mark over whether the SP you've posted here is actually the one you've mapped - since the names are not the same? 正如@JohnOpincar在评论中提到的那样,您在此处发布的SP是否实际上是您已映射的SP是否存在问号-因为名称不相同? My guess is that you've dragged on the wrong SP - and that's only got a RETURN in it, or it returns multiple things and L2S has borked. 我的猜测是您拖错了SP,并且SP中只包含RETURN ,否则它将返回多个内容,并且L2S陷入困境。

Update 2 更新2

Okay - so you've changed the names in the source. 好的-您已经更改了源代码中的名称。 My recommendation is to delete the SP from the L2S designer (and any other manual attempt you may have for this SP), and drag it back on again from the Server Explorer, making sure it's the same server/db you drag it from. 我的建议是从L2S设计器中删除SP(以及对该SP可能进行的任何其他手动尝试),然后从Server Explorer中再次将其重新拖动,以确保它与从中拖动它的服务器/数据库相同。 With the SQL you've posted, L2S will most definitely be able to figure out what the result set is as it's very simple. 使用您发布的SQL,L2S非常简单,因此绝对可以确定结果集是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM