Dapper.Rainbow Database.Insert - removes Id from object - how to override?

437 views Asked by At

I'm using Dapper and Dapper.Rainbow for my ORM (well just switching to it from EF).

My tables have Guids/uniqueidentifiers as Id's.

I'm doing an insert using Dapper Rainbow and it is removing the Id param.

See line 62 of the Insert method... https://github.com/StackExchange/Dapper/blob/master/Dapper.Rainbow/Database.cs

        /// <summary>
        /// Insert a row into the db
        /// </summary>
        /// <param name="data">Either DynamicParameters or an anonymous type or concrete type</param>
        /// <returns></returns>
        public virtual int? Insert(dynamic data)
        {
            var o = (object)data;
            List<string> paramNames = GetParamNames(o);
            paramNames.Remove("Id");

            string cols = string.Join(",", paramNames);
            string colsParams = string.Join(",", paramNames.Select(p => "@" + p));
            var sql = "set nocount on insert " + TableName + " (" + cols + ") values (" + colsParams + ") select cast(scope_identity() as int)";

            return database.Query<int?>(sql, o).Single();
        }

So although the method is marked virtual, how should I override it?

All the other required methods like GetParamNames are internal, so is it best just to clone and run my own version of Dapper.Rainbow?

1

There are 1 answers

2
Void Ray On

Dapper.Rainbow has few limitations:

  • There is no support for composite key mapping.
  • Identity column name for all tables must be called Id.

It expects Id's to be identities, so it removes them...

As an alternative, you can try Dapper-Extensions, which supports both, Integer and Guid primary keys. Or just use Dapper without additional CRUD extensions...