Search code examples
servicestackormlite-servicestack

ServiceStack OrmLite - Handle Auto increment column default seed


How can i set a auto increment column seed from 1 to 100?? in sql server can do this use

 ADD Id INT NOT NULL IDENTITY(1000,1)

but in ormlite autoincrement attribute seems like always start with 1.

tried also

 db.AlterTable<MSchool>("command") // DROP ID AND ADD AUTO INCREMENT COLUMN

it works, if Id doesn't related to any table.

can i set a column autoincrement with default seed and increment?

[AutoIncrement(1000,1)]
public int Id {get;set;}

UPDATE

Resolved, but not good

public class School
{
    [AutoIncrement]
    public int Id {get;set;}
}

//then create table
db.CreateTable<School>();

//then update seed
db.ExecuteSql("DBCC CHECKIDENT ('School',reseed,1000)");

OR

[PostCreateTable("DBCC CHECKIDENT ('School',reseed,1000)")]
public class School : BaseModel
{
    [AutoIncrement]
    public int Id {get;set;}
}

Is there no easier way to do this??


Solution

  • Personally I don't believe this behavior belongs inside source code and would just modify the database out-of-band.

    But if I were to add it in source code I'd do something like:

    if (db.CreateTableIfNotExists<School>())
    {
        db.ExecuteSql("DBCC CHECKIDENT ('School',reseed,1000)");
    }
    

    So it only resets the seed if the table doesn't exist.

    Another option is attach it to the model at runtime so it's decoupled from the class definition with:

    typeof(School)
        .AddAttributes(new PostCreateTableAttribute(
            "DBCC CHECKIDENT ('School',reseed,1000)"));