PawelGerr / Thinktecture.EntityFrameworkCore

These libraries extend Entity Framework Core by a few features to make it easier to work with EF and for easier integration testing or to get more performance in some special cases.
https://dev.azure.com/pawelgerr/Thinktecture.EntityFrameworkCore
BSD 3-Clause "New" or "Revised" License
66 stars 17 forks source link

No store type was specified for the decimal property 'Column1' #38

Closed DavidAllardyce closed 1 year ago

DavidAllardyce commented 1 year ago

I get the following warning on startup. I'm using EF Core 7 on .NET6 with MS SQL.

[WRN] No store type was specified for the decimal property 'Column1' on entity type 'Thinktecture:TempTable:Thinktecture.EntityFrameworkCore.TempTables.TempTable<decimal> (TempTable<decimal>)'. This will cause values to be silently truncated if they do not fit in the default precision and scale. Explicitly specify the SQL server column type that can accommodate all the values in 'OnModelCreating' using 'HasColumnType', specify precision and scale using 'HasPrecision', or configure a value converter using 'HasConversion'.

I checked all of my Decimal columns, but I can't seem to clear it.

Has anyone seen this before?

PawelGerr commented 1 year ago

The lib registers temp tables for most primitive types. Temp table for decimal is one of them. You can provide a default value for precision and scale via ConfigureConventions

public class DemoDbContext : DbContext
{
   protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
   {
      configurationBuilder.Properties<decimal>(builder => builder
                                                          .HavePrecision(18, 5)
                                                          .HaveColumnType("decimal(18, 5)"));
   }

Alternatively, you can configure the property in OnModelCreating.

public class DemoDbContext : DbContext, IDbDefaultSchema
{
   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
      ...
      modelBuilder.ConfigureTempTable<decimal>(builder => builder.Property(t => t.Column1).HasPrecision(18, 5));

You can disable the registration of temp tables when calling AddBulkOperationSupport.

new ServiceCollection()
   .AddDbContext<SqlServerBenchmarkDbContext>(builder => 
        builder.UseSqlServer(sqlServerConnString,
                             optionsBuilder => optionsBuilder.AddBulkOperationSupport(configureTempTablesForPrimitiveTypes: false)
DavidAllardyce commented 1 year ago

Thanks for the detailed answer. Much appreciated!