With a Firebird database, a table was created with the following fields:
/* Table: CLIENTE, Owner: SYSDBA */
CREATE TABLE "CLIENTE"
(
"ID" INTEGER NOT NULL,
"NOME" VARCHAR(50) CHARACTER SET WIN1251 NOT NULL,
CONSTRAINT "PK_CLIENTE" PRIMARY KEY ("ID")
);
and for auto-increment of the field ID one Generator:
/* Triggers only will work for SQL triggers */
SET TERM ^ ;
CREATE TRIGGER "SET_CUST_NO" FOR "CLIENTE"
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
if (new.ID is null) then
new.ID = gen_id(cust_no_gen, 1);
END
^
COMMIT WORK ^
SET TERM ;^
thus, the entire register ID will be incremented.
How to configure Entityframework to work mapping:
Download the packages:
Classes:
1) Model:
public class Cliente
{
public int Id { get; set; }
public string Nome { get; set; }
}
2) Mapping:
public sealed class ClienteConfiguration: EntityTypeConfiguration<Cliente>
{
public ClienteConfiguration()
{
ToTable("CLIENTE");
HasKey(c => c.Id)
.Property(c => c.Id)
.HasColumnName("ID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(c => c.Nome)
.HasColumnName("NOME")
.HasMaxLength(50);
}
}
3) Context
public sealed class Database: DbContext
{
public Database()
:base("FireBirdConnectionString")
{
}
public DbSet<Cliente> Cliente { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ClienteConfiguration());
}
}
4) Configuration of connection:
<connectionStrings>
<add name="FireBirdConnectionString"
connectionString="DataSource=localhost; User=SYSDBA;Password=masterkey; Database=C:\Temp\TUTORIAL.FDB;"
providerName="FirebirdSql.Data.FirebirdClient" />
</connectionStrings>
Your questioning:
How I would map the entity so that the Entity Framework can call Generator for new records?
Was placed .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity), and it worked, it was removed this setting, it also worked, that is to say, the Provider is in charge of executing the Insert with Generator, unshaped.
So, if your table is set to Generator the provider: Firebird Entity Framework Provider does the action and returns you in classe the ID generated.
you’ve tried to put
Identity:[DatabaseGenerated(DatabaseGeneratedOption.Identity)]?– novic
And how will he know that the General of this camp is the
TABLE_EXAMPLO_GEN? @Virgilionovic.– Fernando Leal
So, did you take the test or not, I asked you a question?
– novic
@Virgilionovic, no, why doesn’t it make any sense to function as expected!
– Fernando Leal
Then, without testing, it is difficult to know if the software you use predicts such an operation. A clear example is Oracle, today it works correctly, but at first it didn’t work. Which Provider or package are you using? Can’t say without testing @Fernando.
– novic