Search code examples
c#sql-serverdapperdapper-contrib

Dapper.Contrib - InsertAsync fails to insert an HIERARCHYID key


We have a table in our project with a primary key of HIERARCHYID type:

CREATE TABLE [dbo].[OurTable]
(
    [Id]                HIERARCHYID     NOT NULL,
    <other fields>
)

and the corresponding class to use with Dapper queries:

[Table("OurTable")]
public class OurTable
{
    [ExplicitKey]
    public SqlHierarchyId Id { get; private set; }
    <other fields>
}

The object is created and initialised, the InsertAsync is called, and promptly fails with "Cannot insert Null into Id field", even though debugging shows that Id isn't null and has the correct value at the point where we try to insert.

The [ExplicitKey] attribute was added specifically to fix cases like this, but it looks like it... doesn't? What else can I be missing here?


Solution

  • If you never solved this issue, it may actually be an existing bug in Dapper seen here on GitHub.