Dapper.Contrib icon indicating copy to clipboard operation
Dapper.Contrib copied to clipboard

Insert returns 0 when using a string as an ExplicitKey on an object

Open Adondriel opened this issue 8 years ago • 3 comments

Topic, this may also be related to DapperLib/Dapper#756 but i'm not sure if it's the same issue. Here is the object I am using:

using Dapper.Contrib.Extensions;

[Table("CostBooks.Staff")]
public class Staff
{
    [ExplicitKey]
    public string EmplNbr { get; set; }
    public string AlphaName { get; set; }
    public bool? IsFte { get; set; }
}

Here is the method I am calling. Will try to do this with async and see what happens.

[HttpPost]
public object CreateNewStaff([FromBody]Staff s)
{
    using (IDbConnection db = DBAccessor.Connection())
    {
        if (s.EmplNbr != null)
        {
            var emplNbr = db.Insert<Staff>(s);
            return emplNbr
        }
        else
        {
            return null;
        }
    }
}

Adondriel avatar Jun 12 '17 13:06 Adondriel

Yea, the return type of both methods, is always a number. So by using a string key(Crappy archaic system that I probably wasn't even alive for when it was designed) it breaks the return type and always gives me back a 0. Would be nice if the return type changed based on the key of the type it used... not even sure if that's possible.

Adondriel avatar Jun 12 '17 14:06 Adondriel

Tracking this in 2.0. We need to make the insert methods generic with the key as a type parameter which will resolve this. It's absolutely a breaking change, so tracking it as such.

NickCraver avatar Jun 19 '17 21:06 NickCraver

@Adondriel In my experience it doesn't matter what the key type is because even an Explicit Key of type int will still always return 0 on Insert.

Caltor avatar Feb 19 '18 14:02 Caltor