efcore icon indicating copy to clipboard operation
efcore copied to clipboard

"No store type was specified for the decimal property" warning with `HasConversion<string>()` specified for property when creating a new migration

Open JakeYallop opened this issue 10 months ago • 1 comments

A model containing a decimal property that is converted to a string before being stored in the database gives the following warning when adding a new migration.

No store type was specified for the decimal property 'Value' on entity type 'Entity'. 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'.
using Microsoft.EntityFrameworkCore;

Console.WriteLine("Hello, World!");

public class Context : DbContext
{
    public DbSet<Entity> Entities { get; set; } = null!;

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
            .UseSqlServer("Server=.;Database=Test;Integrated Security=True;");
        base.OnConfiguring(optionsBuilder);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Entity>()
            .Property(x => x.Value)
            .HasConversion<string>();
    }
}

public class Entity
{
    public Guid Id { get; set; }
    public decimal Value { get; set; }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.29" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.29">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.29" />
  </ItemGroup>

</Project>

If you have quite a few of these properties, these warnings hide other important warnings that may pop up when creating migrations.

I was able to replicate this on .NET 6, v6.0.29 and .NET 7, v7.0.18. I could not replicate it using .NET 8, v8.0.4. My real code is still on .NET 6 at the moment.

I couldn't find any specific PR that seemed to be responsible for resolving the issue.

JakeYallop avatar Apr 17 '24 16:04 JakeYallop

Related to #27438

ajcvickers avatar Apr 29 '24 14:04 ajcvickers