DapperLib / DapperLib/Dapper

Dapper try to find Int32 parameters construtor when query returns no row

Open
#1,275 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi,

I have this SQL query

SELECT 
t.Id, t.Name, t.Epoch, t.RightAscension, t.Declination, 
s.TargetId, s.Id, s.FilterIndex, s.Binning, s.Duration, s.Number, s.Completed
FROM Targets t 
LEFT OUTER JOIN Sequences s ON s.TargetId = t.Id"

that I execute with :

using (IDbConnection connection = _dbConnectionFactory.OpenConnection())
{
    var targets = new Dictionary<int, Target>();
	
    return connection.Query<Target, Sequence, Target>(
        sql, (target, sequence) =>
        {
            if (!targets.TryGetValue(target.Id, out Target targetEntry))
            {
                targetEntry = target;
                targets.Add(targetEntry.Id, targetEntry);
            }

            if (sequence != null)
            {
                targetEntry.AddSequence(sequence);
            }

            return targetEntry;
        },
        splitOn: "TargetId").Distinct();
}

As some values are related to a complex object I have this constructor and no parameterless constructor :

private Target(long id, string name, long epoch, double rightAscension, double declination)
{
    Id = id;
    Name = name;
    Coordinates = new Coordinates(rightAscension, declination, epoch);
    Sequences = new List<Sequence>();
}

It works perfectly, except if the query doesn't return any row. In this case I get this error :

A parameterless default constructor or one matching signature (System.Int32 Id, System.Int32 Name, System.Int32 Epoch, System.Int32 RightAscension, System.Int32 Declination) is required.

Why does it try to find a constructor that does not match the types of the table. And why does it try to find a constructor that will never be called (the request does not returns any row) ?

As a workaround I added a constructor that looks like :

private Target(int id, int name, int epoch, int rightAscension, int declination)
{
	throw new InvalidOperationException($"This constructor should not have been called (parameters : id={id}, name={name}, epoch={epoch}, rightAscension={rightAscension}, declination={declination}");
}

Regards,

Julien

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

Reproduce the issue through connection.Query<Target, Sequence, Target> using the shown SQL and Target constructor, comparing an empty result with a result containing rows. Trace the constructor-selection path for Target and determine why the signature reports Int32 parameters; done means the behavior is explained and the unexpected empty-result handling is corrected or covered.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.