For my tests I need to populate the database with known data.
I need to force the Id (primary key) of tables.
Searching the forum I find two method:
a. typeof(LRAPaziente).GetModelMetadata().PrimaryKey.AutoIncrement = false
// insert
typeof(LRAPaziente).GetModelMetadata().PrimaryKey.AutoIncrement = true
b. ModelDefinition md = typeof(LRAPaziente).GetModelMetadata();
var tableName = db.GetDialectProvider().GetQuotedTableName(md);
db.ExecuteSql($“SET IDENTITY_INSERT {tableName} OFF”);
// insert
db.ExecuteSql($“SET IDENTITY_INSERT {tableName} ON”);
I discard the latter as I need to deploy on SQLServer, PostgreSQL and Oracle, so the first is the better approach for me but it does not work.
I’m using SQLServer and it throws the error when IDENTITY_INSERT is OFF it cannot insert value for primary key.This is the POCO:
public class LRAPaziente : DBObject, IHasId<int>
{
[ApiMember(Description = Archivio.ID)]
[Alias("IDAPAZIENTE")]
[AutoIncrement]
public int Id { get; set; }
[ApiMember(Description = Archivio.PID)]
[Alias("PID")]
[StringLength(30)]
[Index(Unique = true)]
public string PID { get; set; }
[ApiMember(Description = Archivio.COGNOME)]
[Alias("COGNOME")]
[StringLength(30)]
public string Cognome { get; set; }
[ApiMember(Description = Archivio.NOME)]
[Alias("NOME")]
[StringLength(30)]
public string Nome { get; set; }
}