FirebirdSQL / NETProvider

Firebird ADO.NET Data Provider
https://www.firebirdsql.org/en/net-provider/
Other
160 stars 66 forks source link

Decimal truncation on Database First Approach #1104

Open victorvilella opened 1 year ago

victorvilella commented 1 year ago

Hi guys.

I had a problem with an project with Database First Approach. My app was truncating every decimal's information.

Consider model as:

class Sell 
{
    public int SellerId { get; set; }
    public decimal Total { get; set; }
}

and Context as:

// This context was generated by 'dotnet ef scaffold' command
class MainContext 
{
    public DbSet<Sell> Sells { get;  set; }
    modelBuilder.Entity<Sell>(entity =>
    {
       entity.Property(e => e.Total)
                .HasColumnType("DECIMAL")
                .HasColumnName("TOTAL");
    }
}

When I perform an insert/update operation, it just saves the integer part of decimal.

var sell = new Sell {
    SellerId = 1.
    Total = 323.99
};
context.Sells.Add(sell);
await context.SaveChangesAsync(); // It just saved 323

Looking for a solution I realized the problem was column type generated by ef scaffolding proccess. I adjusted my context as follows:

// This context was generated by 'dotnet ef scaffold' command
class MainContext 
{
    public DbSet<Sell> Sells { get;  set; }
    modelBuilder.Entity<Sell>(entity =>
    {
       entity.Property(e => e.Total)
                .HasColumnType("NUMERIC(10, 3)")
                .HasColumnName("TOTAL");  
      // I changed DECIMAL to NUMERIC with precision present on table's field.
    }
}

I would like to ask to fix this issue and I give my problem and how I solved this problem.

Thanks in advance.

fdcastel commented 1 year ago

It seems the problem is in this method.

The current code is ignoring several metadata about the columns, like

@cincuranet with your approval I would like to start working on a PR to fix all this.

And also to add sequences (generators) support to scaffolding.

I already did a similar work on Firebird dialect for SQLAlchemy implementation. For reference, this is the metadata query we use.

cincuranet commented 1 year ago

Go ahead.

fdcastel commented 1 year ago

One last question: Should I branch it from ef7 branch? (say yes, please)

cincuranet commented 1 year ago

You can, but I do not promise that branch to be super stable.

fdcastel commented 1 year ago

Understood. No problems. 👍