Patrick
Patrick

Reputation: 2781

Store and manage SQL Server data types

I need to store and manipulate the following model with LINQ and store the data in SQL Server Express:

I set the property like this:

public DbSet<Carga> Cargas { get; set; }

public class Carga
{
    public decimal PesoLiquido { get; set; }
}

To manipulate values like: 1 / 1.0 / 1.10 / 1.012

What is the correct type in C# and Sql-Server?

I get errors like:

public ActionResult Index()
{
    return View(db.Cargas.ToList());
}

The 'PesoLiquido' property on 'Carga' could not be set to a 'Single' value. You must set this property to a non-null value of type 'Decimal'.

Upvotes: 0

Views: 92

Answers (2)

Ben
Ben

Reputation: 2484

Seems that you have fixed precision data type in C# (decimal) and float precision on sql column (real or float).

Either change sql column type to money/decimal/numeric type or change C# field type to Single.

if you deal with money or cargo weight decimal would be the best - it can store all range of values without precision lost.

Default decimal on sql server is decimal(18,0). If you plan to use another scale/precision, let entity framework know: modelBuilder.Entity<Carga>().Property(c=> c.PesoLiquido).HasPrecision(16, 10);

Upvotes: 1

Gert Arnold
Gert Arnold

Reputation: 109079

The datatype 'Single' is the same as float in C#. There is no implicit conversion from float to decimal. Make sure that decimal values are set by the client.

Upvotes: 0

Related Questions