My c# object has a decimal property:
public decimal LastPrice { get; set; }
While processing my object, the decimal value gets set. For example:
LastPrice = 0.091354;
I modified my DbContext to increase the decimal precision as explained in another stackoverflow post:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var property in modelBuilder.Model.GetEntityTypes()
.SelectMany(t => t.GetProperties())
.Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?)))
{
property.SetColumnType("decimal(38, 10)");
}
}
The tables design view in Microsoft Sql Server Management Studio reflects this configuration. Here is the table scripted from SSMS:
CREATE TABLE [dbo].[TradedCurrencyPairs](
[Id] [int] IDENTITY(1,1) NOT NULL,
[LastPrice] [decimal](38, 10) NOT NULL,
...
When I check the object during debugging as it gets added to my DbContext, it looks good:
But in the database it ends up as being 0.0000000000.
As far as I can tell, the value is still being rounded as if it would have a precision of 2. A value that should be 0.09232 becomes 0.0900000000. So all decimals still get cut.
I have tried several different Data Annotations:
// [PrecisionAndScale(20, 10)]
//[RegularExpression(@"^\d+\.\d{20,10}$")]
//[Range(0, 99999999999999999999.9999999999)]
[Range(typeof(decimal), "20", "10")]
but they didn't help. Inserting data from SSMS works fine: INSERT INTO TradedCurrencyPairs VALUES ('tzt', 'ttt', 'rrrr', '20120618 10:34:09 AM' , 'hgghghg', 0.123456, 1, 0.123456789, 0.123456, 0.123456); go
My DbModelSnapshot for the column looks like this:
b.Property<decimal>("LastPrice")
.HasPrecision(10)
.HasColumnType("decimal(20, 10)");
I also tried:
TradedCurrencyPair TestPair = new TradedCurrencyPair("one", "two", "Bibox", DateTime.Now, "unknown", 0.1234567890M, 1, 0.1234567890M, 0.1234567890M, 0.1234567890M);
context.TradedCurrencyPairs.Add(TestPair);
context.SaveChanges();
The result is the same...
Somewhere between setting the value and it ending up in the database, it gets modified :/
here is the SQL Table:
/****** Object: Table [dbo].[TradedCurrencyPairs] Script Date: 25/07/2020 09:32:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TradedCurrencyPairs](
[Id] [int] IDENTITY(1,1) NOT NULL,
[TradedCurrency] [nvarchar](max) NULL,
[BaseCurrency] [nvarchar](max) NULL,
[Exchange] [nvarchar](max) NULL,
[DateTime] [datetime2](7) NOT NULL,
[TransactionType] [nvarchar](max) NULL,
[LastPrice] [decimal](20, 10) NOT NULL,
[ExchangeInternalPairId] [int] NOT NULL,
[High24h] [decimal](20, 10) NOT NULL,
[Low24h] [decimal](20, 10) NOT NULL,
[Volume24h] [decimal](20, 10) NOT NULL,
CONSTRAINT [PK_TradedCurrencyPairs] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
What DOES work is not using entity framework:
SqlCommand command = new SqlCommand("insert into TradedCurrencyPairs values('one', 'two', 'Bibox', convert(datetime, '18-06-12 10:34:09 PM', 5), 'unknown', 0.1234567890, 1, 0.1234567890, 0.1234567890, 0.1234567890); ", cnn);
This way the decimals do not get modified. So EF causes the issue.
Could anybody please explain to me what I am doing wrong?
Thanks a lot!