How to define Generator (from Firebird) for an Entity framework 6 model field?

Asked

Viewed 451 times

1

For example:

I have the following table:

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

And the following Generator:

TABLE_EXAMPLO_GEN

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

In Nhibernate it was necessary something like this, 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 it’s possible?

Note: I already have (in legacy system) a Firebird database in this format, with Generator for primary keys (no triggers).

  • you’ve tried to put Identity: [DatabaseGenerated(DatabaseGeneratedOption.Identity)]?

  • And how will he know that the General of this camp is the TABLE_EXAMPLO_GEN? @Virgilionovic.

  • So, did you take the test or not, I asked you a question?

  • @Virgilionovic, no, why doesn’t it make any sense to function as expected!

  • 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.

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 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.

  • 1

    I understood what you proposed, I had even thought about something like this, only that this is exactly what I didn’t want because I am working on a legacy database that already works in Delphi and in . net with Nhibernate, and it would not be the first option to create Triggers for all keys of the tables =. But still thanks for the suggestion +1 =D. I’ll wait to see if there is a solution like that of Nhibernate.

  • Just one question, is there another way? to generate the numbers? is that I did research and was the only way I found ...? Not with TRIGGERS?

  • 1

    I don’t know! That’s exactly my question and doubt with that question! I also did a long search before posting here and did not succeed, so I decided to try here on Sopt and on ONLY to get a better idea of the problem and possible solutions (such as the one you suggested!).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.