DapperLib / DapperLib/Dapper

FatalExecutionEngineError when using struct and SqlMapper on 1.50.2

Open
#773 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

Since updating to 1.50.2 i'm getting an error with mapping to structs using SqlMapper

FatalExecutionEngineError occurred
  HResult=-2146233082
  Message=Exception of type 'System.ExecutionEngineException' was thrown.
  InnerException: 

I was able to reproduce this is a very simple scenario in a console application:

var connection = new SqlConnection("Server=localhost;Database=TestDb;User Id=user;Password=password;");

var sql = "SELECT * FROM Dogs WHERE Id = @Id";

var result = SqlMapper.Query<Dog, SubData, Dog>(connection, sql, (dog, subdata) =>
  {
    dog.subData = subdata;
    return dog;
  }, new { Id = 1 }, splitOn: "Age");

Model definitions:

public class Dog
{
  public int Id { get; set; }
  public string Name { get; set; }
  public SubData subData { get; set; }
}

public struct SubData
{
  public int Age { get; set; }
  public int Breed { get; set; }
}

The SQL-table is just 4 columns (nothing fancy)

Id (bigint)
Name (nvarchar(50)
Age (int)
Breed (int)

I tried debugging SqlMapper.cs and found the exception is thrown in the SqlMapper.GenerateMapper method. Actually when trying to execute the Func:

((Func<TFirst, TSecond, TReturn>)map)((TFirst)deserializer(r), (TSecond)otherDeserializers[0](r));

When I remove the Breed property from the struct, it does work. So it seems to break when the struct contains more than one property.

I'm not sure how to look any further into this.
Does anyone have any idea what this could be?

Same issue on 1.50.0
1.40.0 all working fine.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the struct-mapping example and inspect SqlMapper.cs, especially GenerateMapper and the failing Func invocation. Compare behavior between versions 1.40.0 and 1.50.0 with a struct containing both Age and Breed. Done means the query completes without FatalExecutionEngineError while mapping all struct properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.