Thunderstruck icon indicating copy to clipboard operation
Thunderstruck copied to clipboard

Guid for Primary Key

Open GusBeare opened this issue 10 years ago • 2 comments

Hi

I am trying to use the DataObject to insert a row into a table that has a Guid PK. It works great with an incrementing Id. But once I switched to a Guid I can't get it to work. I've tried setting the pk like this in the initialisation:

var userData = new DataObject<Users>("Users","Guid"); model.Guid = Guid.NewGuid(); userData.Insert(model);

But that doesn't work I get a sql error saying Guid cannot be null.

"Cannot insert the value NULL into column 'Guid', table 'Nancy.dbo.Users'; column does not allow nulls. INSERT fails."

What am I doing wrong?

Gus

ps: I found this code in DataRuntimeObject.cs which looks a bit dubious. See the "Id" bit.

public PropertyInfo GetPrimaryKey(string primaryKeyName) { if (_primaryKey == null) { var validProperties = GetValidProperties(); if (primaryKeyName != null) return validProperties.FirstOrDefault(p => p.Name == primaryKeyName); _primaryKey = validProperties.FirstOrDefault(p => p.Name == "Id" ) ?? validProperties.First(); }

        return _primaryKey;
    }

GusBeare avatar Oct 22 '14 16:10 GusBeare

As far as I know, Thunderstruck only supports integer primary key fields using IDENTITY columns.

bdemarzo avatar Jan 26 '15 16:01 bdemarzo

yes, thanks, it would be nice if it could handle GUID's because IDENTITY types are troublesome for me when it comes to merging databases. But I don't have a good enough grasp of the code to be able to enhance it.

GusBeare avatar Jan 26 '15 17:01 GusBeare