0

I have a simple desktop application that is using SQL Server Compact (3.5 and about to be upgraded to 4.0).

In my main customers table, in addition to the ID column - which is the PK and is auto incremented, I need to hold a Number column that starts in some number (20000) and is unique.

The option of computing the next number by the existing highest row is not a good one - if other client is adding a customer between my first selection to my inserting - I'll have a problem.

As pointed in many places, SqlCE auto incremented column should be Identity column, and in each table could be only one Identity column.

Is there other practice you know to solve this problem?

BTW - I am using Linq to ADO .net.

Edit:

Just found this link and discovered that I can use even the Identity option with linq to Entities framework: http://erikej.blogspot.com/2010/04/solutions-to-server-generated-keys-and.html

Community
  • 1
  • 1
Roy Tsabari
  • 2,000
  • 6
  • 26
  • 41

1 Answers1

0

this action is wrong basically but if you persist that you need a transaction when you select max value of your column.so you ensure another user can't update(and change max value) until this action complete.

using (TransactionScope transaction = new TransactionScope())
{         
    // Set new NumericColumn 
    entity.NumericColumn = (db.Context.Rotations.Max(r => (long?)r.NumericColumn ) ?? 20000) + 1;
    // Insert entity
    db.Context.Rotations.AddObject(entity);
    db.Context.SaveChanges();

    transaction.Complete();
}
Kohányi Róbert
  • 9,791
  • 4
  • 52
  • 81