This creates the table with the correct Identity Seed, but when inserting I get the following exception
Cannot insert explicit value for identity column in table 'TableName' when IDENTITY_INSERT is set to OFF
I’m assuming that because the property does not have an [AutoIncrement] attribute it’s trying to insert the value of the Id and then throwing a wobbly because it is an identity field.
If I add the [AutoIncrement] attribute then it errors on table creation, because it already has an identity field.
SET IDENTITY_INSERT is only valid for the session it is run in, so needs to be set before and after the INSERT statement, if this is done it just sets the Id to 0.
The problem is that the Id column of the table has the Identity set to true via the custom field ( INT IDENTITY(1000, 1) ), but because the column does not have the [AutoIncrement] attribute OrmLite is trying to execute an INSERT statement that includes the Id column, which in turn causes the error to be returned.
Right, in that case an easier solution might be to execute a DDL statement, e.g:
var modelDef = typeof(TableType).GetModelMetadata();
var tableName = db.GetDialectProvider().GetQuotedTableName(modelDef);
db.ExecuteSql($$"DBCC CHECKIDENT ({tableName}, RESEED, 1000)");
Which you could also couple to the creation of the table using a Custom SQL Hook, e.g:
[PostCreateTable("DBCC CHECKIDENT (TableType, RESEED, 1000)")]
public class TableType
{
[AutoIncrement]
public int Id { get; set; }
}
db.CreateTable<TableType>();