robconery/massive — Issue #128: Insert doesn't return ID when Primary Key is uniqueidentifier
Author: deevus
Created: 2012-02-14 08:06:27+00:00
State: open

If you have a table where the Primary Key is ```uniqueidentifier``` the use of ```@@IDENTITY``` will never return the new inserted value.

I had an idea to change the stub variable in ```CreateInsertCommand```:
```var stub = "INSERT INTO {0} ({1}) \r\n OUTPUT inserted.{2} \r\n VALUES ({3})";```

Which would then format the sql as so:
```var sql = string.Format(stub, TableName, keys, PrimaryKeyField, vals);```

Then just change ```Insert``` to use ```ex.ID = cmd.ExecuteScalar();``` and remove the call to ```ExecuteNonQuery()```

The only problem being that if the table doesn't provide a primary key or one wasn't set when inheriting ```DynamicModel``` this will break. Currently I have overridden ```CreateInsertCommand``` and ```Insert``` myself for the specific table with a ```uniqueidentifier``` PK, but I thought maybe there could be a solution that solves the problem of non-INT PK's all round.