实体框架核心 - 使用存储过程和输出参数

时间:2017-07-22 09:33:21

标签: asp.net entity-framework entity-framework-core

我正在尝试将存储过程与新的Entity Framework Core一起使用。我需要很快启动新项目,这将是ASP.Net 5,但不确定实体框架是否适合这项工作。应用程序将每分钟触发几个存储过程,我需要输出参数。 EF会对此有用还是应该使用ADO.Net?

我尝试过FromSql和database.ExecuteSqlCommand,但没有运气。

using (AppDbContext db = factory.Create())
        {
            var in1 = new SqlParameter
            {
                ParameterName = "ParamIn1",
                DbType = System.Data.DbType.Int64,
                Direction = System.Data.ParameterDirection.Input
            };
            var in2 = new SqlParameter
            {
                ParameterName = "ParamIn2",
                DbType = System.Data.DbType.String,
                Direction = System.Data.ParameterDirection.Input
            };
            var out1 = new SqlParameter
            {
                ParameterName = "ParamOut1",
                DbType = System.Data.DbType.Int64,
                Direction = System.Data.ParameterDirection.Output
            };
            var out2 = new SqlParameter
            {
                ParameterName = "ParamOut2",
                DbType = System.Data.DbType.String,
                Direction = System.Data.ParameterDirection.Output
            };

            var result = db.Database.ExecuteSqlCommand("exec spTestSp", in1, in2, out1, out2);
        }

3 个答案:

答案 0 :(得分:9)

它应该可以工作,但我相信你还需要在命令语句中包含参数名称和> fd <- data_frame(Intro1 = c(NA,NA,NA, 1:5), Intro2 = c(NA,1:5, NA,NA)) > fd %>% mutate(Con = case_when( ! Intro1 %in% c(2,4) ~ 0, ! Intro2 %in% c(2,4) ~ 1 )) # A tibble: 8 x 3 Intro1 Intro2 Con <int> <int> <dbl> 1 NA NA 0 2 NA 1 0 3 NA 2 0 4 1 3 0 5 2 4 NA 6 3 5 0 7 4 NA 1 8 5 NA 0 关键字

OUT

答案 1 :(得分:2)

@Nkosi和@Whistler部分描述了上述解决方案的完整示例,以便于以后的读者阅读!

在我的示例中,我试图执行数据库中存在的存储过程以获取特定路径。

string tableName = "DocumentStore";
string path;

        var in1 = new SqlParameter
        {
            ParameterName = "TableName",
            Value = tableName,
            Size = Int32.MaxValue,
            DbType = System.Data.DbType.String,
            Direction = System.Data.ParameterDirection.Input
        };

        var out1 = new SqlParameter
        {
            ParameterName = "Path",
            DbType = System.Data.DbType.String,
            Size = Int32.MaxValue,
            Direction = System.Data.ParameterDirection.Output
        };

        //_context is DbContext Object
        _context.Database.ExecuteSqlCommand("EXEC GetFileTableRootPath @TableName, @Path OUT", in1,out1);

        path = out1.Value.ToString();

答案 2 :(得分:0)

StoredProcedureEFCore扩展支持输出参数

然后用法是这样

List<Model> rows = null;

ctx.LoadStoredProc("dbo.ListAll")
   .AddParam("limit", 300L)
   .AddParam("limitOut", out IOutParam<long> limitOut)
   .Exec(r => rows = r.ToList<Model>());

long limitOutValue = limitOut.Value;

ctx.LoadStoredProc("dbo.ReturnBoolean")
   .AddParam("boolean_to_return", true)
   .ReturnValue(out IOutParam<bool> retParam)
   .ExecNonQuery();

bool b = retParam.Value;

ctx.LoadStoredProc("dbo.ListAll")
   .AddParam("limit", 1L)
   .ExecScalar(out long l);