我试图找出是哪一列或哪几列跳出了以下错误。由第三方服务提供的传入数据发生了一些变化,当我尝试将其保存到SQL时,它现在导致失败。
Microsoft.EntityFrameworkCore.DbUpdateException:发生错误 在更新条目时。有关详细信息,请参见内部异常。 -> System.Data.SqlClient.SqlException:算术溢出错误 将数字转换为数据类型数字。该声明已经 终止。
一个非常简单的流程:
数据结构定义为:
[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转换为datetime)。
示例传入的失败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
}
答案 0 :(得分:0)
我为批次失败时创建了故障转移迭代保存。因为我将此类的数据库连接设置为“全局”,所以我无意中遇到了问题。我将添加记录集并尝试保存它(错误弹出),然后尝试迭代越来越小的批次以发现有问题的记录。问题是我没有在迭代前删除有问题的记录集。因此,每次迭代都会在DB连接中携带错误条件!
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中有注释的部分),错误立即弹出!
在此数据批中保存特定记录时出错! “ [{\” block_chain \“:\”以太坊\“,\” block_reduction \“:\” \“,\” block_reward \“:\” 0 \“,\” block_time \“:0.0,\” consensus_method \ “:\” \“,\”十进制\“:18.0,\” difficulty_retarget \“:\” \“,\” genesis_address \“:\” 0x3520ba6a529b2504a28eebda47d255db73966694 \“,\” hash_algorithm \“:\” \“,\ “ minemine \”:\“ False \”,\“ p2p_port \”:0:\“ rpc_port \”:0,\“ token_role \”:\“ \”,\“ float \”:0.0,\“ minted \ “:60000000000000000000000000.0,\”总计_“:60000000000000000000000000.0,\” max_supply \“:60000000000000000000000000.0,\”钱包\“:\” \“,\” genesis_timestamp \“:0.0}]”“ Microsoft.EntityFrameworkCore.DbUpdateException:发生错误 在更新条目时。有关详细信息,请参见内部异常。 -> System.Data.SqlClient.SqlException:算术溢出错误 将数字转换为数据类型数字。该声明已经 终止。
尽管C#可以使用DECIMAL数据类型处理60000000000000000000000000.0,但我们的SQL定义为DECIMAL(28,6)。由于6位数字的精度仅留出10 ^ 22值的空间。
(似乎SQL现在可以处理DECIMAL(38,6)。可以在不丢失生产数据的情况下使用列定义。)