使用存储库模式在ASP.NET Core中执行存储过程

问题描述

我正在尝试执行存储过程,该过程应该仅将int返回给调用方。

但是执行后,我得到空值。我正在尝试使用ASP.NET Core MVC中的存储库模式执行存储过程,如果您能对此提供帮助,我将非常高兴。

您可以在下面找到我代码的一部分

控制器(已验证的变量返回null)

private readonly IUnitOfWork _unitOfWork;
private Homeviewmodel HomeVM;

public HomeController(IUnitOfWork unitOfWork)
{
    _unitOfWork = unitOfWork;
}
     
public IActionResult Index(ApplicationUser applicationUser)
{
    var parameter = new DynamicParameters();
    parameter.Add("@UserID",applicationUser.Id);
    var verified = _unitOfWork.SP_Call.OneRecord<UserSP>(SD.usp_RemainingCredit,parameter);

    HomeVM = new Homeviewmodel()
                 {
                      UserList = _unitOfWork.User.GetAll(),};
    return View(HomeVM);
}

UserSp.cs

public class UserSP
{
    public int Expired { get; set; }
    public int RemainingCredit { get; set; }
}

Homeviewmodel.cs

public ApplicationUser ApplicationUser { get; set; }
public UserSP UserSP { get; set; }
public IEnumerable<ApplicationUser> UserList { get; set; }
public IEnumerable<UserSP> UserSPList { get; set; }

和我的SP_Call来自存储库类:

public class SP_Call : ISP_Call
{
    private readonly ApplicationDbContext _db;
    private static string ConnectionString = "";

    public SP_Call(ApplicationDbContext db)
    {
        _db = db;
        ConnectionString = db.Database.GetDbConnection().ConnectionString;
    }

    public void dispose()
    {
        _db.dispose();
    }

    public void Execute(string procedureName,DynamicParameters param = null)
    {
        using (sqlConnection sqlCon = new sqlConnection(ConnectionString))
        {
            sqlCon.open();
            sqlCon.Execute(procedureName,param,commandType: System.Data.CommandType.StoredProcedure);
        } 
    }

    public IEnumerable<T> List<T>(string procedureName,DynamicParameters param = null)
    {
        using (sqlConnection sqlCon = new sqlConnection(ConnectionString))
        {
            sqlCon.open();
            return sqlCon.Query<T>(procedureName,commandType: System.Data.CommandType.StoredProcedure);
            }
        }
    }

    public Tuple<IEnumerable<T1>,IEnumerable<T2>> List<T1,T2>(string procedureName,DynamicParameters param = null)
    {
        using (sqlConnection sqlCon = new sqlConnection(ConnectionString))
        {
            sqlCon.open();
            var result = sqlMapper.QueryMultiple(sqlCon,procedureName,commandType: System.Data.CommandType.StoredProcedure);
            var item1 = result.Read<T1>().ToList();
            var item2 = result.Read<T2>().ToList();

            if (item1 != null && item2 != null)
            {
                return new Tuple<IEnumerable<T1>,IEnumerable<T2>>(item1,item2);
            }
        }

        return new Tuple<IEnumerable<T1>,IEnumerable<T2>>(new List<T1>(),new List<T2>());
    }

    public T OneRecord<T>(string procedureName,DynamicParameters param = null)
    {
        using (sqlConnection sqlCon = new sqlConnection(ConnectionString))
        {
            sqlCon.open();
            var value = sqlCon.Query<T>(procedureName,commandType: System.Data.CommandType.StoredProcedure);
            return (T)Convert.ChangeType(value.FirstOrDefault(),typeof(T));
        }
    }

    public T Single<T>(string procedureName,DynamicParameters param = null)
    {
        using (sqlConnection sqlCon = new sqlConnection(ConnectionString))
        {
            sqlCon.open();
            return (T)Convert.ChangeType(sqlCon.ExecuteScalar<T>(procedureName,commandType: System.Data.CommandType.StoredProcedure),typeof(T));
        }
    }
}

我要执行的存储过程:

ALTER PROCEDURE [dbo].[usp_RemainingCredit]
    (@UserID nvarchar(450))
AS 
BEGIN
    DECLARE @RemainingCredit int,@Number_count int

    SET @Number_count = ISNULL ((SELECT Number 
                                 FROM [dbo].[View_UserCreditControll] 
                                 WHERE Id = @UserID AND CreditType = 0),-1)

    IF (@Number_count <> -1)
        SET @RemainingCredit = @Number_count - (SELECT COUNT(*) FROM [dbo].[View_UserTrackQuery] WHERE Id = @UserID)
    ELSE IF (@Number_count = -1 )
        SET @RemainingCredit = -1

    RETURN @RemainingCredit
END

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)