Using DataTable as a Table Valued Parameter in EF Core 2.0

前端 未结 1 1536
温柔的废话
温柔的废话 2021-01-13 17:36

[Updated problem description] We have a bulk import process for which we were passing IEnumerable as a Table Valued Parameter (TVP) to a St

1条回答
  •  心在旅途
    2021-01-13 18:20

    The only possible error I could find in your code is the SqlParameter's TypeName. Yours is not fully qualified, I needed to include the schema, i.e. "dbo.".

    I'm using EFCore 2.0.2 and EFCore.SqlServer 2.0.2

    This is my code:

    DataTable table = new DataTable();
    table.Columns.Add(new DataColumn("FieldId", typeof(int)));
    table.Columns.Add(new DataColumn("Value", typeof(double)));
    foreach (Value v in NewRows)
    {
        DataRow row = table.NewRow();
        row["FieldId"] = v.FieldId;
        row["Value"] = v.Value;
        table.Rows.Add(row);
    }
    
    var param = new SqlParameter("@replacementValues", table) { TypeName = "dbo.CustomSqlType", SqlDbType = SqlDbType.Structured };
    await _dbContext.Database.ExecuteSqlCommandAsync("EXEC dbo.UpdateValues @replacementValues", param);
    

    0 讨论(0)
提交回复
热议问题