dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.69k stars 3.17k forks source link

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

Open JakeYallop opened 5 months ago

JakeYallop commented 5 months ago

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.

ajcvickers commented 5 months ago

Related to #27438