gpt4 book ai didi

sql-server - 将数字转换为数字数据类型时出现算术溢出错误。英孚核心

转载 作者:行者123 更新时间:2023-12-05 07:23:28 30 4
gpt4 key购买 nike

我正在尝试找出是哪一列或哪些列触发了以下错误。由第 3 方服务提供的传入数据发生了一些变化,现在当我尝试将其保存到 SQL 时导致失败。

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Arithmetic overflow error converting numeric to data type numeric. The statement has been terminated.

一个非常简单的流程:

  • 以 JSON 格式读取第 3 方 API
  • 使用newtonsoft直接转换为EF数据模型类
  • 将记录添加到数据库然后保存。

数据结构定义为:

[JsonObject(MemberSerialization.OptIn)]
public class RatDbAttributes
{
[JsonProperty]
[StringLength(50)]
public string block_chain { get; set; } // varchar(50)
[JsonProperty]
[StringLength(50)]
public string block_reduction { get; set; } // varchar(50)
[JsonProperty]
[StringLength(50)]
public string block_reward { get; set; } // varchar(50)
[JsonProperty]
public double block_time { get; set; } // decimal(28,6)
[JsonProperty]
[StringLength(100)]
public string consensus_method { get; set; } // varchar(100)
[JsonProperty]
public decimal decimals { get; set; } // decimal(28,6)
[JsonProperty]
[StringLength(50)]
public string difficulty_retarget { get; set; } // varchar(50)
[JsonProperty]
[StringLength(200)]
public string genesis_address { get; set; } // varchar(200)
[JsonProperty]
[StringLength(100)]
public string hash_algorithm { get; set; } // varchar(100)
[JsonProperty]
[StringLength(50)]
public string mineable { get; set; } // varchar(50)
[JsonProperty]
public long p2p_port { get; set; } // bigint
[JsonProperty]
public long rpc_port { get; set; } // bigint
[JsonProperty]
[StringLength(200)]
public string token_role { get; set; } // varchar(200)
[JsonProperty]
public decimal @float { get; set; } // decimal(28,6)
[JsonProperty]
public decimal minted { get; set; } // decimal(28,6)
[JsonProperty]
public decimal total_supply { get; set; } // decimal(28,6)
[JsonProperty]
public decimal max_supply { get; set; } // decimal(28,6)
[JsonProperty]
[StringLength(133)]
public string wallet { get; set; } // varchar(133)
[JsonProperty]
[NotMapped]
public double genesis_timestamp { get; set; } // see below

[JsonIgnore]
public DateTime Genesis_TimeStamp { get { return genesis_timestamp.ToDateTime(); } set { genesis_timestamp = value.ToEpoch(); } }


// Foregin Key Relationship (1-to-1) and Primary Key
[JsonIgnore]
public long TokenMasterId { get; set; }
[JsonIgnore]
[ForeignKey("TokenMasterId")]
public RatDbTokenMaster TokenMaster { get; set; } //foreign key to Parent
}

我已经仔细检查了 genesis_timestamp,这不是问题所在(将 double 转换为日期时间)。

示例传入的失败 JSON:

{"block_chain":""
,"block_reduction":""
,"block_reward":"0"
,"block_time":0.0
,"consensus_method":""
,"decimals":0.0
,"difficulty_retarget":""
,"genesis_address":""
,"hash_algorithm":""
,"mineable":"False"
,"p2p_port":0
,"rpc_port":0
,"token_role":""
,"float":0.0
,"minted":0.0
,"total_supply":0.0
,"max_supply":0.0
,"wallet":""
,"genesis_timestamp":0.0
}

最佳答案

我为批处理失败时创建了故障转移迭代保存。因为我为这个类(class)设置了“全局”数据库连接,所以我无意中遇到了问题。我会添加记录集并尝试保存它(错误弹出),然后尝试迭代越来越小的批处理以发现有问题的记录。问题是我在迭代之前没有删除有问题的记录集。因此,每次迭代都携带了数据库连接中的错误条件!

    internal void IterateSave<TModel>(List<TModel> items) where TModel : class
{
using (LogContext.PushProperty("Data: Class", nameof(RatBaseCommandHandler)))
using (LogContext.PushProperty("Data: Method", nameof(IterateSave)))
using (LogContext.PushProperty("Data: Model", nameof(items)))
{
int max = items.Count;
int skip = 0;
int take = (max > 20) ? (max / 5) : 1;
int lastTake = take;
List<TModel> subItems = new List<TModel>();

while (skip <= max)
{
try
{
subItems = items.Skip(skip).Take(take).ToList();
Log.Verbose("Working {Max} | {Take} | {Skip}", max, take, skip);

skip += take;
_db.Set<TModel>().AddRange(subItems);
_db.SaveChanges();
}
catch (Exception ex)
{
/***** Was not removing the faulty record/recordset! *****/
_db.Set<TModel>().RemoveRange(subItems);
/***** Was not removing the faulty record/recordset! *****/

if (take == 1 && skip < max)
{
Log.Error(ex, "Error saving specific record in this data batch! {GuiltyRecord}", JsonConvert.SerializeObject(subItems));
if (skip >= max - 1)
{
depth--;
return;
}
}
else if (take > 1)
{
Log.Warning("Something is wrong saving this data batch! {RecordCount} Running a smaller batch to isolate.", take);
IterateSave(subItems);
}
}
}
}
}

添加了这两行(catch 中的注释部分),错误立即弹出!

Error saving specific record in this data batch! "[{\"block_chain\":\"Ethereum\",\"block_reduction\":\"\",\"block_reward\":\"0\",\"block_time\":0.0,\"consensus_method\":\"\",\"decimals\":18.0,\"difficulty_retarget\":\"\",\"genesis_address\":\"0x3520ba6a529b2504a28eebda47d255db73966694\",\"hash_algorithm\":\"\",\"mineable\":\"False\",\"p2p_port\":0,\"rpc_port\":0,\"token_role\":\"\",\"float\":0.0,\"minted\":60000000000000000000000000.0,\"total_supply\":60000000000000000000000000.0,\"max_supply\":60000000000000000000000000.0,\"wallet\":\"\",\"genesis_timestamp\":0.0}]" Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Arithmetic overflow error converting numeric to data type numeric. The statement has been terminated.

虽然 C# 可以处理 DECIMAL 数据类型的 60000000000000000000000000.0,但我们的 SQL 是在 DECIMAL(28,6) 定义的。由于 6 位精度只为 10^22 值留出空间。

(看来 SQL 现在可以处理 DECIMAL(38,6)。是时候在不丢失生产数据的情况下使用列定义了。)

关于sql-server - 将数字转换为数字数据类型时出现算术溢出错误。英孚核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56010059/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com