| Milos How-To |
| Using Integer (Identity) Primary Keys |
public class EmployeeBO : BusinessObject { protected override void Configure() { this.MasterEntity = "Employees" ; this.PrimaryKeyField = "EmployeeID" ; this.PrimaryKeyType = EPS.Data.KeyType.IntegerAutoIncrement; } }
The important part is the very last line of this sample, which indicates to the business object that an auto-increment primary key is to be used.
Note that there are some far-reaching implication when using identity keys. Whenever an identity key is used, data that is created on the client machine needs to be assigned a temporary key until that data is saved to the database, which will trigger the actual creation of the real identity key. Milos automatically creates temporary keys on the client. All these temporary keys are negative (minus) values (and are therefore easy to spot). Once the data gets saved to the database, SQL Server (or whatever the database may be) will automatically create real identity keys. At that point, the data on the client is out of sync with the data on the server. While "real" keys are used on the server, the client still has the temporary keys. Milos will therefore query the database to retrieve the keys that were actually generated and replace the temporary key in the updated row, so the row can later be re-identified. There are a few important aspects to this however:
(See also: HowTo_CreateChildItemsInEntities).