C# 插入时删除的刻度编号(超过2位)
我定义了如下表列:C# 插入时删除的刻度编号(超过2位),c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我定义了如下表列: Column name: Lat DataType: decimal(9,6) 以及我的模型定义: [DisplayFormat(DataFormatString = "{0:000.000000}", ApplyFormatInEditMode = true)] [Column(TypeName = "decimal")] public decimal Lat { get; set; } [如上所述,我手动将列更改为十进制(9,6)] 我使用EF
Column name: Lat
DataType: decimal(9,6)
以及我的模型定义:
[DisplayFormat(DataFormatString = "{0:000.000000}", ApplyFormatInEditMode = true)]
[Column(TypeName = "decimal")]
public decimal Lat { get; set; }
[如上所述,我手动将列更改为十进制(9,6)]我使用EF6在数据库中创建新记录
当我想为
Lat
插入值35.6997311时,数据库中的结果是35.690000(4位数字转换为零)。
为什么EF删除数字并插入零而不是它们?如何在数据库中插入完整的数字?
我的控制器插入操作:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Lat,CreatedAt")] Example example)
{
if (ModelState.IsValid)
{
db.Agencies.Add(example);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(example);
}
可以创建一个属性来缩放数字 像这样创建一个
DecimalPrecision
属性
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class DecimalPrecision : Attribute
{
public byte precision { get; set; }
public byte scale { get; set; }
public DecimalPrecision(byte precision, byte scale)
{
this.precision = precision;
this.scale = scale;
}
public static void ConfigureModelBuilder(DbModelBuilder modelBuilder)
{
modelBuilder.Properties().Where(x => x.GetCustomAttributes(false).OfType<DecimalPrecision>().Any())
.Configure(c => c.HasPrecision(c.ClrPropertyInfo.GetCustomAttributes(false).OfType<DecimalPrecision>().First()
.precision, c.ClrPropertyInfo.GetCustomAttributes(false).OfType<DecimalPrecision>().First().scale));
}
}
用法强>
在属性上添加DecimalPrecision
属性
[DisplayFormat(DataFormatString = "{0:000.000000}", ApplyFormatInEditMode = true)]
[Column(TypeName = "decimal")]
[DecimalPrecision(9,6)]
public decimal Lat { get; set; }
可以创建一个属性来缩放数字 像这样创建一个
DecimalPrecision
属性
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class DecimalPrecision : Attribute
{
public byte precision { get; set; }
public byte scale { get; set; }
public DecimalPrecision(byte precision, byte scale)
{
this.precision = precision;
this.scale = scale;
}
public static void ConfigureModelBuilder(DbModelBuilder modelBuilder)
{
modelBuilder.Properties().Where(x => x.GetCustomAttributes(false).OfType<DecimalPrecision>().Any())
.Configure(c => c.HasPrecision(c.ClrPropertyInfo.GetCustomAttributes(false).OfType<DecimalPrecision>().First()
.precision, c.ClrPropertyInfo.GetCustomAttributes(false).OfType<DecimalPrecision>().First().scale));
}
}
用法强>
在属性上添加DecimalPrecision
属性
[DisplayFormat(DataFormatString = "{0:000.000000}", ApplyFormatInEditMode = true)]
[Column(TypeName = "decimal")]
[DecimalPrecision(9,6)]
public decimal Lat { get; set; }
+1.我只是在打同样的东西。实体框架的默认十进制精度为(18,2)。您正在将您的c#十进制转换为18,2,这将删除除前两个小数以外的所有小数,然后将其显示为#.#############然后显示为#.#.然后将其显示为:
找不到类型或命名空间名称DecimalPrecisionAttribute…
@Kevin@Rainman“db.agents.Add(示例)“db”是您的上下文。找到上下文类,如果OnModelCreating不存在,请按照我的解释重写它。+1。只是在打同样的东西。实体框架的默认十进制精度为(18,2)。您正在将您的c#十进制转换为18,2,这将删除除前两个小数以外的所有小数,然后将其显示为#.#############然后显示为#.#.然后将其显示为:找不到类型或命名空间名称DecimalPrecisionAttribute…
@Kevin@Rainman“db.agents.Add(示例)“db”是您的上下文。找到上下文类,如果OnModelCreating不存在,请按照我的解释重写它。