79167747

Date: 2024-11-07 19:01:58
Score: 1.5
Natty:
Report link

I know this is an old post, but in case anyone was wondering, there is a workaround for this which involves using SqlDataReader and mapping columns manually by column index/order. Here's the sample code I'm using to map results for a procedure that has some columns with duplicate names:

    var resultList = new List<JobInfoModel>();
    await using var connection = new SqlConnection(_dbOptions.Value.DbConnectionString);
    await connection.OpenAsync(cancellationToken);
    using var command = new SqlCommand("JT_JobGet", connection);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@ClientID", clientId);
    command.Parameters.AddWithValue("@UserID", userId);
    command.Parameters.AddWithValue("@JobID", jobId);
    
    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            try
            {
                var dataItem = new JobInfoModel
                {
                    Id = reader.GetInt32(0),
                    Oldid = reader.IsDBNull(1) ? null : reader.GetInt32(1),
                    ClientId = reader.GetInt32(2),
                    StatusId = reader.GetInt32(3),
                    SiteId = reader.GetInt32(4),
                    ContactName = reader.IsDBNull(5) ? null : reader.GetString(5),
                    ContactNumber = reader.IsDBNull(6) ? null : reader.GetString(6),
                    ContactEmail = reader.IsDBNull(7) ? null : reader.GetString(7),
                    Description = reader.GetString(8),
                    PriorityId = reader.GetInt32(9),
                    FaultCategoryId = reader.GetInt32(10),
                    ContractorId = reader.IsDBNull(11) ? null : reader.GetInt32(11),
                    ScheduledDate = reader.IsDBNull(12) ? null : reader.GetDateTime(12),
                    JobTypeId = reader.IsDBNull(13) ? null : reader.GetInt32(13),
                    ContractorEmailed = reader.IsDBNull(14) ? null : reader.GetString(14),
                    ContractorCalled = reader.IsDBNull(15) ? null : reader.GetString(15),
                    ContractorAcknowledgedJob = reader.IsDBNull(16) ? null : reader.GetString(16),
                    ContractorAttended = reader.IsDBNull(17) ? null : reader.GetString(17),
                    ContractorRef = reader.IsDBNull(18) ? null : reader.GetString(18),
                    CapexRef = reader.IsDBNull(19) ? null : reader.GetString(19),
                    RequestedByUserId = reader.IsDBNull(20) ? null : reader.GetInt32(20),
                    RequestedDate = reader.IsDBNull(21) ? null : reader.GetString(21),
                    CreatedByUserId = reader.IsDBNull(22) ? null : reader.GetInt32(22),
                    CreatedDate = reader.IsDBNull(23) ? null : reader.GetDateTime(23)
            };

            resultList.Add(dataItem);
        }
        catch (Exception ex)
        {
            _logger.LogCritical("Failed to map SP result to class {Exception}", ex);
        }
    }
}
await connection.CloseAsync();

IsDBNull method checks if the column is nullable, otherwise it will throw an exception if the column value is null during conversion to desired type.

For running the SQL query you may need to create the reader this way:

using var reader = await connection.ExecuteReaderAsync("SELECT created_timestamp AS CreatedDate, imported_timestamp AS CreatedDate FROM Orders WHERE OrderId = @OrderId");
Reasons:
  • Blacklisted phrase (2): was wondering
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Pukanium