SqlMapperExtensions.cs error when Computed Id property defined where ExplictKey set on other property
Opened this issue · 0 comments
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.