I'm struggling with how to properly map output parameters back to an object using Dapper, when I create DynamicParamters from a template object.
var parameters = new DynamicParameters(entity);
parameters.Add("@Id", dbType: DbType.Int32, direction: ParameterDirection.Output);
parameters.Output(entity, x => x.Id);
await conn.ExecuteAsync(
"TodoItemInsert", entity,
commandType: CommandType.StoredProcedure);
My hope was that the above code would map the resulting ID back into the entity that I created the parameters from in the first place. No matter what I try I am unable to get the parameter to return from the stored procedure. Calling parameters.Get<int>("@Id") throws a KeyNotFoundException. parameters.Get<int?>("@Id") returns null.
My SQL is a very basic insert sproc
ALTER PROCEDURE [dbo].[TodoItemInsert]
@Name VARCHAR(255)
, @Description VARCHAR(512)
, @DueDate DATETIME = NULL
, @IsComplete BIT = 0
, @Id INT OUTPUT
AS
INSERT INTO
[TodoItems]
(
Name
, Description
, DueDate
, IsComplete
)
VALUES
(
@Name
, @Description
, @DueDate
, @IsComplete
)
SET @Id = SCOPE_IDENTITY()
What is the correct way to get an output parameter back from Dapper when trying to use an object template for my DynamicParameters?