0

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?

Will
  • 773
  • 1
  • 7
  • 24

1 Answers1

0

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...

Void Ray
  • 9,849
  • 4
  • 33
  • 53
  • Hi @Void-Ray - I think you've misunderstood my question problem.My Id's are called Id and I am not using composite key mapping. My Id's are Guid's, which I set in code(not sql), and Rainbow doesn't honour the set Id value on insert. – Will Aug 01 '18 at 13:22
  • Right, as far as I can tell, it only supports numeric identity (SQL server will auto-assign the next value on insert) Id that is why it removes it. – Void Ray Aug 01 '18 at 14:18