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 field ID
a 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 ;^
In this way, every register ID
will be incremented.
How to configure EntityFramework to work with mapping:
Download the packages:
Classes:
1) Template:
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) Connection setup:
<connectionStrings>
<add name="FireBirdConnectionString"
connectionString="DataSource=localhost; User=SYSDBA;Password=masterkey; Database=C:\Temp\TUTORIAL.FDB;"
providerName="FirebirdSql.Data.FirebirdClient" />
</connectionStrings>
Your question:
How would I map the entity so that the Entity Framework can call the generator for new records?
It has been placed .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
, and it worked, it was removed this configuration, it also worked, that is , the provider is in charge of executing the insert with Generator
, without configuration.
So, if your table is configured with Generator
provider
: Firebird Entity Framework Provider
does the action and returns you in classe
ID
generated.