How to set the generator (from firebird) to an Entity framework 6 model field?

1

For example:

I have the following table:

TABLE_EXAMPLO 
-----------
ID         
NAME       

And the following Generator:

TABLE_EXAMPLO_GEN

How would I map the entity so that the Entity Framework can call the generator for new records?

In NHibernate something like this was needed in ClassMap<T> :

Id(t => t.Id).GeneratedBy.Native("TABLE_EXAMPLO_GEN").Column("ID");
// ou
Id(t => t.Id).GeneratedBy.Sequence("TABLE_EXAMPLO_GEN").Column("ID");

And in EntityFramwork is this possible?

  

Note: I already have (in the legacy system) a database in Firebird in that format, with generator for the primary keys (without triggers).

    
asked by anonymous 12.09.2016 / 20:36

1 answer

1

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.

    
13.09.2016 / 02:15