I have inherited some code from another developer and I'm very confused on the proper technique for getting the next value for an integer type primary key field. Version 2.6
Here is the code that exists but it fails on some calls based on which field I'm looking for:
public static int GetNewKey(string keyName)
{
var utlKeysEntity = new UtlkeysEntity(keyName);
if (utlKeysEntity.IsNew)
{
utlKeysEntity.KeyName = keyName;
utlKeysEntity.CurrentValue = 0;
}
utlKeysEntity.CurrentValue++;
utlKeysEntity.Save();
return utlKeysEntity.CurrentValue;
}
The exception is in the set for the KeyName property of the UtlkeysEntity class.
"The value specified will cause an overflow error in the database. Value length: 13. Column max. length: 10 Parameter name: KeyName"
The key I'm passing in is 13 characters (DescriptionID).
I am also inheriting a database and don't want to modify all the tables to use identities. I can't see how this code can work, what if you have two tables with the same primary key field name?
Thanks!!!