-
Notifications
You must be signed in to change notification settings - Fork 10
Guid for Primary Key #8
Description
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","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;
}