how to set a primary key initial value

Oct 12, 2011 at 2:35 PM

Hello,

I have an entity like below

[Entity(KeyScheme.Identity)]
public class MyEntity
{
  [Field(IsPrimaryKey = true)]
  public int id { get; set; }
  
  [Field(SearchOrder = FieldSearchOrder.Ascending)]
  public string Name { get; set; }
}

and when I insert the entity to the database

MyEntity e = new MyEntity();
e.Name = "entity name";
Store.Insert(e);

the primary key will be created automatically and the first value will be 1, the second value will be 2 etc. But I want to start the primary key not from 1 but 1000, for example, and the second value should be 1001 etc.

I understand, that I can set KeySheme.None and realize that behavior manually, but I hope that there is a way to do it automatically. Am I right or not?

 


Coordinator
Oct 12, 2011 at 4:51 PM

So you'd like to set the "seed" value for the identity field when the table is created by the ORM?  I'm working on the ORM this week - let me see if I can get that in.

Oct 12, 2011 at 7:21 PM

I want to use the database on few devices and my idea is to include a serial number of the device into the primary key (id). So the high word of the id will contain the serial and the low word of the id will be number of the record for the entity.  I will also use desktop database and records from devices will be copied to that desktop database, and these serials allow me to recognize wich record was downloaded from exact device.

For example, Device_1 has two records:

  • id = 0x00010001, name = "name_1"
  • id = 0x00010002, name = "name_2"

Device_2 has three records

  • id = 0x00020001, name = "name_1"
  • id = 0x00020002, name = "name_2"
  • id = 0x00020003, name = "name_3"

These records will be copied to the desktop database

  • id = 0x00010001, name = "name_1"
  • id = 0x00010002, name = "name_2"
  • id = 0x00020001, name = "name_1"
  • id = 0x00020002, name = "name_2"
  • id = 0x00020003, name = "name_3"

So it is easy to say with records where read from each device. And my question is how to add the serial automaticaly?

Coordinator
Oct 14, 2011 at 9:32 PM

THis could potentially be done with seeding the auto-increment PK filed, but it's not how I would do it myself, nor how I would recommend it.  Add a field to the table that indicates the device ID.  If you really want to do it in the PK filed, you're probably best served by using a KeyScheme=None and then having your own internal routine that generates and tracks PK values for you.

Oct 17, 2011 at 11:54 AM

OK. Thanks.