Efcore: What is correct way to add decimal precision

Created on 16 Nov 2017  路  1Comment  路  Source: dotnet/efcore

I have a model that I would like to create a decimal precision of decimal(38,20) but cant seen to find the correct answer online. below is my entity and the decimal precision i want is on the LastPriceBtc

after i add a migration, i tried changing the field in the modelsnapshot to b.Property<decimal?>("LastPriceBtc").HasColumnType("decimal(38,20)"); but that didnt work.

I would just like to be able to add an attribute to the entity/model and not worry about anything else

 public class Coin
 {
    [Key]
    public int CoinId { get; set; }

    public int Rank { get; set; }

    [Required]
    [StringLength(500)]
    public string Symbol { get; set; }


    [Required]
    [StringLength(500)]
    public string Name { get; set; }

    [Column(TypeName = "Money")]
    public decimal? LastPriceUsd { get; set; }

    public decimal? LastPriceBtc { get; set; }

    public DateTime? LastUpdated { get; set; }

}
closed-question

Most helpful comment

Use this code
C# [Column(TypeName = "decimal(38, 20)")] public decimal? LastPriceBtc { get; set; }
Or what you put in modelsnapshot, you can put the same fluent API in DbContext.OnModelCreating method.

ModelSnapshot is snapshot of your model when last migration created. It is only used to compute changes between last scaffolded migration and current model to generate next migration. ModelSnapshot is not supposed to be edited by user. And editing it will not change your model.

>All comments

Use this code
C# [Column(TypeName = "decimal(38, 20)")] public decimal? LastPriceBtc { get; set; }
Or what you put in modelsnapshot, you can put the same fluent API in DbContext.OnModelCreating method.

ModelSnapshot is snapshot of your model when last migration created. It is only used to compute changes between last scaffolded migration and current model to generate next migration. ModelSnapshot is not supposed to be edited by user. And editing it will not change your model.

Was this page helpful?
0 / 5 - 0 ratings