[英]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: 我需要使用LINQ存储和操作以下模型,并将数据存储在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 要像这样操纵值:1 / 1.0 / 1.10 / 1.012
What is the correct type in C# and Sql-Server? C#和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.
“ Carga”上的“ PesoLiquido”属性无法设置为“ Single”值。 You must set this property to a non-null value of type 'Decimal'.
您必须将此属性设置为'Decimal'类型的非空值。
Seems that you have fixed precision data type in C# (decimal) and float precision on sql column (real or float). 似乎您在C#中具有固定的精度数据类型(十进制),而在sql列上具有浮点精度(实数或浮点数)。
Either change sql column type to money/decimal/numeric type or change C# field type to Single. 将sql列类型更改为money / decimal / numeric类型,或将C#字段类型更改为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). sql server上的默认十进制是十进制(18,0)。 If you plan to use another scale/precision, let entity framework know:
modelBuilder.Entity<Carga>().Property(c=> c.PesoLiquido).HasPrecision(16, 10);
如果您打算使用其他比例/精度,请让实体框架知道:
modelBuilder.Entity<Carga>().Property(c=> c.PesoLiquido).HasPrecision(16, 10);
The datatype 'Single' is the same as float
in C#. 数据类型“ Single”与C#中的
float
相同。 There is no implicit conversion from float
to decimal
. 从
float
到decimal
没有隐式转换。 Make sure that decimal
values are set by the client. 确保客户端设置了
decimal
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.