EF Core Db First: 'Value was either too large or too small for an Int16' Error

75 views Asked by At

I am developing a web application using .NET 5. I am using Oracle 12 as the database and generating entities with Oracle.EntityFrameworkCore v.5.21.

I have a table TableA in the database, and it has a column of type Number(5). This column is used to store numerical values, and some of these values are above 95000.

When creating the entity, EF Core interprets this column as short, and when I query the database using EF Core, I encounter an error

Value was either too large or too small for an Int16

The short data type can only hold values up to 32767, and the values in the database range between 95000 and 96000. I can not change database column to Number(6) datatype.

A new DbContext class is recreated with each scaffolding, so I extended this class with the OnModelCreatingPartial method in a different file. However, I still encounter the same error when accessing entity data.

My actual DbContext is PortalDbContext which is in PortalDbContext.cs. I also created a file MyPortalDbContext.cs in which I have:

public partial class PortalDbContext 
{
    partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Personnel>()
                    .Property(e => e.IdNum)
                    .HasColumnType("NUMBER(5)")
                    .HasConversion<int>();
    }
}

I also tried to use ApplyConfiguration as:

public partial class PortalDbContext 
{
    partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration(new PersonnelConfiguration());         
    }
}

public class PersonnelConfiguration : IEntityTypeConfiguration<Personnel>
{
    public void Configure(EntityTypeBuilder<Personnel> builder)
    {
        builder.Property(e => e.IdNum)
               .HasColumnType("NUMBER(5)")
               .HasConversion<int>();
    }
}

These two did not work.

What I am expecting is getting IdNum database column as int or decimal in the entity, even if it is defined as Number(5) in the Oracle database.

0

There are 0 answers