DapperLib / DapperLib/Dapper.Contrib

SqlException on hidden parent property

Open
#120 2 comments 2 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

When using the new modifier on a property within a child class both the property from the parent and child class are added to the SQL parameters causing an exception to be thrown as parameter names must be unique.

For example:

    public class MyParentClass
    {
        [Key] int Id { get; set; }
        public int Value { get; set; }
    }

    [Table("MyChildTable")]
    public class MyChildClass : MyParentClass
    {
        public new string Value { get; set; }
    }

    public class DapperStuff
    {
        private readonly IDbConnection db;
        public DapperStuff(IDbConnection db)
        {
            this.db = db;
        }
        public void DoInsert(MyChildClass insert)
        {
            this.db.Insert<MyChildClass>(insert); 
            // SqlException: The variable name '@Value' has already been declared. Variable names must be unique within a query batch or stored procedure.
        }
    }

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 at the Insert call in DoInsert and trace how inherited properties are collected into SQL parameters. Reproduce the example with MyParentClass and MyChildClass, then inspect the generated parameter names. Done means the insert no longer declares duplicate @Value parameters and the example succeeds without SqlException.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.