2

When executing stored procedure without return value but with a user-defined table type like this:

var dbStrings = new SqlParameter("@data", SqlDbType.Structured)
{
    TypeName = new StringListType().Name,
    Value = new StringListType().DataRecordsFrom(listOfStrings.Select(p => p.Value).ToList())
 };
_dbContext.Database.ExecuteSqlCommand("exec [dbo].[InsertRecords] @SID, @filerType, @data", sid.ToString(), "Field", dbStrings);

I get an error

When executing a command, parameters must be exclusively database parameters or values

How can I execute stored procedure through DbContext with multiple input parameters (some UDTT)?

1

1 Answer 1

3

In this case you need to (as error message tells you) create an array of SqlParameter and pass this when calling ExecuteSqlCommand.

var sqlParams = new SqlParameter[]
{
    new SqlParameter("@SID", sid.ToString()),
    new SqlParameter("@filterType", "Pin"),
    new SqlParameter("@data", SqlDbType.Structured)
    {
        TypeName = new StringListType().Name,
        Value = new StringListType().DataRecordsFrom(listOfStrings.Select(p => p.Value).ToList())
    }
};

_dbContext.Database.ExecuteSqlCommand("exec [dbo].[InsertRecords] @SID, @filterType, @data", sqlParams);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.