System.ArgumentException: The table type parameter must have a valid type name

后端 未结 3 540
南方客
南方客 2021-01-01 09:28

I am trying to pass in a user defined table type into a query in C#.

the type is defined with 2 columns (org and sub org)

this is what my code looks like:

相关标签:
3条回答
  • 2021-01-01 09:58

    You can get this error also when you wanna pass table params into stored procedure. There is happen if you use entity famework Context.Database.SqlQuery(). You must necessary set TypeName property for your table params.

    SqlParameter codesParam = new SqlParameter(CODES_PARAM, SqlDbType.Structured);
                SqlParameter factoriesParam = new SqlParameter(FACTORIES_PARAM, SqlDbType.Structured);
    
                codesParam.Value = tbCodes;
                codesParam.TypeName = "[dbo].[MES_CodesType]";
                factoriesParam.Value = tbfactories;
                factoriesParam.TypeName = "[dbo].[MES_FactoriesType]";
    
    
                var list = _context.Database.SqlQuery<MESGoodsRemain>($"{SP_NAME} {CODES_PARAM}, {FACTORIES_PARAM}"
                    , new SqlParameter[] {
                       codesParam,
                       factoriesParam
                    }
                    ).ToList();
    
    0 讨论(0)
  • 2021-01-01 10:01

    Set mapping to your type in SqlServer using TypeName property that: Gets or sets the type name for a table-valued parameter, that has to fix .

    p.TypeName = "dbo.MyType";
    

    Check as well Table-Valued Parameters post

    0 讨论(0)
  • 2021-01-01 10:03

    Note that this may also happen when you're executing a stored procedure and you don't have the SqlCommand.CommandType set to CommandType.StoredProcedure, as such:

    using (SqlCommand cmd = new SqlCommand("StoredProcName", conn))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
    }
    
    0 讨论(0)
提交回复
热议问题