点网核心中的存储过程 [英] Stored Procedure in dot net core

查看:88
本文介绍了点网核心中的存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的方法,该方法调用存储过程。

Below is my method, which calls a stored procedure.

public List<int> GetActivityListforUser(string userId)
{
    IList<int> results = new List<int>();            
    context.LoadStoredProc("dbo.GetRegionOrganizationActivities")
            .WithSqlParam("userId", userId)
            .ExecuteStoredProc((handler) =>
            {    
                results = handler.ReadToList<int>().ToList();
            });

    return results.ToList();
}

我的存储过程 dbo.GetRegionOrganizationActivities 仅返回一列ID,这是传递参数userId所需的结果。

My stored procedure dbo.GetRegionOrganizationActivities, returns only one column Id, which is the required result up on passing the parameter userId.

我的问题在以下行中:

return results.ToList();

我可以看到来自存储过程的所有列表,但是所有int值均为0。 b $ b列表计数与存储的proc结果计数匹配,但是值应为示例:1、2、3等。它显示0、0、0等。

I can see all the list that comes from the stored procedure, but all int values are 0. The list count matches with stored proc result count, but values should be example: 1,2,3, etc. it shows 0,0,0, etc.

有人可以提供我做错事情的一些见识吗?

Can any one provide some insight on what I am doing wrong?

推荐答案

似乎您正在使用Snickler.EFCore nuget

It seems you are using the Snickler.EFCore nuget package with EF Core.

简短的回答:
您必须创建一个复杂的对象以允许该库映射您的结果集,例如:

Short answer: You'll have to create a complex object to allow this library to map your result set, something like:

public class RegionOrganizationActivity
{
    public int Id { get; set; }
}

然后可以使用它来提取整数列表:

then you can use this to extract your list of integers:

public List<int> GetActivityListforUser(string userId)
{
    IList<RegionOrganizationActivity> results = new List<RegionOrganizationActivity>();            
    context.LoadStoredProc("dbo.GetRegionOrganizationActivities")
            .WithSqlParam("userId", userId)
            .ExecuteStoredProc((handler) =>
            {    
                results = handler.ReadToList<RegionOrganizationActivity>();
            });

        return results.Select(activity => activity.Id).ToList();
    }

说明:

handler.ReadToList< int>()在这里不起作用,因为 ReadToList< T> 实现仅支持复杂类型。假设它需要创建 T 的实例,然后尝试将属性与列匹配。

handler.ReadToList<int>() won't work here because the ReadToList<T> implementation only supports complex types. It is assuming it needs to create an instance of T and try to match properties to the columns.

因为在那当 T int 时没有匹配的属性,后一部分将失败。这就解释了为什么要获得所有0值的方法:该方法设法执行的唯一操作是 Activator.CreateInstance< int>(),该值将返回0。

Because there are no properties to match when T is int, the latter part fails. This explains why you are getting all values of 0: the only thing the method manages to do is an Activator.CreateInstance<int>() which will return 0.

其他帮助方法 ReadToValue 仅支持单个结果值,也将不起作用。

Their other helper method ReadToValue only supports a single result value, which won't work either.

这意味着您不能使用该库将结果直接映射到 List< int> 。因此,您需要使用复杂的对象来匹配结果集。

This means you can't use this library to map your result straight to a List<int>. So you'll need to use complex object to match the result set.

这篇关于点网核心中的存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆