0

I'm having a problem inserting a decimal value into the database using PetaPoco.

In my model I have the proeprty:

    [Column("Price")]
    public decimal PriceTotal { get; set; }

and in the databse the relevant column Price is of type decimal(18, 2)

When I do the insert:

public void InsertModel(MyViewModel model)
    {
        _database.Insert(model);
    }

I'm getting an error:

System.InvalidCastException: 'Invalid cast from 'System.Decimal' to 'System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.'

Not sure how else I could do that? doesn't decimal translate to sql server's decimal(18,2)?

nickornotto
  • 1,946
  • 4
  • 36
  • 68
  • You don't need to translate anything. Did you *read* the error message? You are trying to save or read a decimal into an integer field – Panagiotis Kanavos Jun 23 '17 at 10:01
  • Post the *full* exception, including its call stack, the table schema and a *reproducible* code example. Perhaps you defined `ID` as `numeric(18,2)` in the databse and `int` in your class, resulting in an error when the ORM tries to retrieve the auto-generated ID – Panagiotis Kanavos Jun 23 '17 at 10:05
  • I wrote exactly what I have. Not sure if treating me like an idiot is a solution. – nickornotto Jun 23 '17 at 10:28

1 Answers1

0

You can use this decimal?

[Column("Price")]
public decimal? PriceTotal { get; set; }
Tien Nguyen Ngoc
  • 1,555
  • 1
  • 8
  • 17
  • That's not what the error is about. You don't need a nullable decimal to store data in a decimal database field – Panagiotis Kanavos Jun 23 '17 at 10:03
  • Thanks. This doesn't work - it simply turned out it was an error on another field which was integer, don't know why it shouted about decimal while it wasn't related at all to the only decimal property I have in the model. – nickornotto Jun 23 '17 at 10:31