2

I have a problem with saving a decimal in EntityFramework (Model First). In my EDMX I declared my proprierty as a Decimal(30,10) then I tried to save the number: '1215867935736100000'

The result is:

Parameter value '1215867935736100000' is out of range.

I tried to make an INSERT query with SQL Server 2008 R2 Management Studio

INSERT INTO MyTable (MyColumn) VALUES (1215867935736100000)

and the record is saved correctly.

Do you have any idea?

Thanks, Max

Nethuns
  • 23
  • 1
  • 3
  • 1
    What is the data type generated on the table in sql server? – Kaido Nov 28 '13 at 11:16
  • I think same issue has been solved here, follow link below http://stackoverflow.com/questions/1455547/sql-server-decimal30-10-losing-last-2-decimals – Nimit Vachhani Nov 28 '13 at 11:19
  • Your precision is far too large. What type of data are you storing in that column? You should get yourself familiar with how [precision, scale & length](http://msdn.microsoft.com/en-us/library/ms190476.aspx) works in SQL server. – James Nov 28 '13 at 13:20

1 Answers1

2

This could be an issue with the version of EF that you are using.

I have a code first version of the problem and can almost reproduce the error you are getting. The error I get includes a .00 in the message

{"Parameter value '1215867935736100000.00' is out of range."}

The way I solved it was to add .HasPrecision(30,10) to the EntityTypeConfiguration class.

this.Property(t => t.value)
    .HasColumnName("value")
    .HasPrecision(30,10) //this line resolves the problem in my test code
    .IsRequired();

This doesn't fix your problem but I can at least confirm it is possible for Entity Framework to write the value 1215867935736100000 to the database.

CREATE TABLE [dbo].[TestTable1](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [value] [decimal](30, 10) NOT NULL
) ON [PRIMARY]

The test code:

MyContext testTableContext = new MyContext();
TestTable1 testTable1 = new TestTable1();
testTable1.value = 1215867935736100000;
testTableContext.TestTable1.Add(testTable1);
testTableContext.SaveChanges();
qujck
  • 14,388
  • 4
  • 45
  • 74
  • SQL server doesn't recognize the `m` suffix, that's used by C# to differentiate between a `double`/`decimal` (or `float`). – James Nov 28 '13 at 13:16
  • @james the title and the tags mention a thing called `Entity Framework` and @Enthuns has stated he can insert the data with raw Sql - a C# solution is required. Try reading the question. – qujck Nov 28 '13 at 13:32
  • Firstly, I have read the question. Secondly, the point you make in your answer is only applicable if the number is a *real* literal i.e. contains decimal places (which this value is not). Thirdly, the issue is on trying to save to the DB, not populating the EF model, therefore it indicates an issue at the DB server not the client. – James Nov 28 '13 at 13:44