ExecutionEngineException thrown when mapping with IDataReader to DTO with a different datatype for a property
See original GitHub issueFrom @janv8000 on November 19, 2012 8:29
An ExecutionEngineException without any useful debug information is thrown when executing the following code. This only happens with a Release build without a debugger attached. When debugging the data comes out just fine.
Versions used: Automapper NuGet version 2.2.0 .NET 4
Database schema:
CREATE TABLE [dbo].[Foo](
[Bar] [numeric](4, 0) NOT NULL,
[Baz] [nvarchar](50) NOT NULL
)
INSERT [dbo].[Foo] ([Bar], [Baz]) VALUES (CAST(0 AS Numeric(4, 0)), N'0')
INSERT [dbo].[Foo] ([Bar], [Baz]) VALUES (CAST(112 AS Numeric(4, 0)), N'112')
Code:
class Program
{
public class Foo
{
public int Bar { get; set; }
public string Baz { get; set; }
}
static void Main(string[] args)
{
//Build as release and X86 or Any CPU
var stringBuilder = new StringBuilder();
stringBuilder.Append("SELECT Bar , ");// Bar is actually a numeric(4,0) but declared in dto as int
stringBuilder.Append(" Baz ");
stringBuilder.Append("FROM Foo");
List<Foo> result;
using (var connection = new SqlConnection("Server=localsqlalias;Database=AutoMapperTest;Integrated Security=True;"))
{
connection.Open();
using (var sqlCommand = new SqlCommand(stringBuilder.ToString(), connection))
{
using (var sqlDataReader = sqlCommand.ExecuteReader())
{
Mapper.CreateMap<IDataReader, IEnumerable<Foo>>();
result = Mapper.Map<IDataReader, IEnumerable<Foo>>((IDataReader)sqlDataReader).ToList();
}
}
}
foreach (var aResult in result)
{
Console.WriteLine("{0} {1}", aResult.Bar, aResult.Baz);
}
Console.WriteLine("Press enter to quit");
Console.ReadLine();
}
}
Copied from original issue: AutoMapper/AutoMapper#273
Issue Analytics
- State:
- Created 9 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
AutoMapper: Mapping between a IDataReader and DTO ...
I have a DataReader which contains the result of a stored procedure. The naming convention for the columns use underscores for spaces. I...
Read more >ExecutionEngineException Class (System)
The exception that is thrown when there is an internal error in the execution engine of the common language runtime. This class cannot...
Read more >SqlDataReader.cs source code in C# .NET
Int16)); DataColumn DataType = new DataColumn(SchemaTableColumn.DataType, typeof(System.Type)); DataColumn ProviderSpecificDataType = new ...
Read more >C#, PHP, C++, iOS, MySQL, SQL, ASP.NET, Objective-C, ...
NET MVC 3, UITableView, CodeIgniter, validation, shell, API, Google Maps, ... ColdFusion, Spring Security, model, datatable, Devise, Android AsyncTask, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jbogard Issue still exists. Is this going to be fixed in version 4.1?
Temporary Workaround: In my SQL whenever there is a datatype of int, long, decimal,float, double, I am casting it as a string and then I am creating an explicit .ForMember mapping for those columns. It’s painful but works.