AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

No store type was specified for decimal property 'Price' on entity type 'Movie'

Open AlbertLi255 opened this issue 11 months ago • 3 comments

Description

It pops up "No store type was specified for decimal property 'Price' on entity type 'Movie'", after create class "SeedData" according to the document

Crtl + F5 to run and can't be successed the prompt is here:

image

Https to run can be successed but still have that warning the prompt is here:

warn: Microsoft.EntityFrameworkCore.Model.Validation[30000] No store type was specified for the decimal property 'Price' on entity type 'Movie'. 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'. info: Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (15ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT CASE WHEN EXISTS ( SELECT 1 FROM [Movie] AS [m]) THEN CAST(1 AS bit) ELSE CAST(0 AS bit) END

image

Page URL

https://learn.microsoft.com/zh-cn/aspnet/core/tutorials/first-mvc-app/working-with-sql?view=aspnetcore-8.0&tabs=visual-studio

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/tutorials/first-mvc-app/working-with-sql.md

Document ID

a0b5e15c-d747-8710-95fb-a55c09861a1e

Article author

@wadepickett


Associated WorkItem - 251073

AlbertLi255 avatar Mar 14 '24 01:03 AlbertLi255

Resolved I think this is a good solution to deal with "no specify the column type"

use Data Annotations to declare a default on your model. The data annotation looks like: [Column(TypeName = "decimal(18,2)")]

Reference to

  1. https://mattferderer.com/entity-framework-no-type-was-specified-for-the-decimal-column
  2. https://github.com/dotnet/AspNetCore.Docs/issues/8414

AlbertLi255 avatar Mar 14 '24 07:03 AlbertLi255

(I updated this) @AlbertLi255, thanks for reporting this, very appreciated.

In the Initial migration section of the previous tutorial in this series, there is a note that the warning is expected, since there are steps in the tutorial series later to take:

Add a Model, section Initial migration

  • The Update-Database command generates the following warning:

    • No store type was specified for the decimal property 'Price' on entity type 'Movie'. 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'.

    • Ignore the preceding warning, it's fixed in a later tutorial.

What is happening here: The process of updating the model for both Price and Rating are covered here: Add a new field

However, this makes this 10 tutorial series more complicated than it needs to be, getting a warning that is not addressed until 3 tutorials later is disconcerting, even if it is just a warning, and it is called out and indicated it will be addressed later.

I will work on trying to make those steps work a bit smoother, there has been too much confusion generated over this step.

wadepickett avatar Mar 14 '24 16:03 wadepickett

Looking back there have been a number of community-filed issues in the past over the confusion associated with this section for what is a high traffic topic

wadepickett avatar Apr 30 '24 21:04 wadepickett

Closing per Rick's feedback after review.

wadepickett avatar Jun 27 '24 01:06 wadepickett