DapperLib / DapperLib/Dapper.Contrib

SqlMapperExtensions.cs error when Computed Id property defined where ExplictKey set on other property

Open
#118 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
293
Forks
109
PR merge metrics
No merged PRs in 30d

Description

There is an issue when an id property is defined in a class where another property on the class has an explicit key.
Consider this scenario where we are using a computed property to set a 'standard' Id property where a table uses a non-standard primary key naming.

using Dapper.Contrib.Extensions;

namespace MyNamespace
{
    [Table("ABC_Table")]
    public class MyTable 
    {
        [ExplicitKey]
        public int SomePrimaryKeyId { get; set; }
        [Computed]
        public int Id { get => SomePrimaryKeyId; set => SomePrimaryKeyId = value; }
        public string OtherProp { get; set; }
    }
}

This causes an issue in GetSingleKey() when KeyPropertiesCache does not find a [Key] attr and tries to find an implicit key using the id property name. The KeyPropertiesCache returns Id as a key but the ExplicitKeyPropertiesCache also returns the named key.

In the situation where an implicit key is found, it should also check if any ExplicitKey exists.
Note this does not have a problem when SomePrimaryKeyId is decorated with [Key], only [ExplicitKey] causes an issue.

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 in SqlMapperExtensions.cs at GetSingleKey() and inspect how KeyPropertiesCache and ExplicitKeyPropertiesCache are consulted for the shown model shape. Verify the completed change handles an implicit Id alongside an ExplicitKey without treating both as keys, including the case where the nonstandard property has [ExplicitKey].

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
databases
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.