我正在尝试从dot和api获取加密价格,并使用dotnet core 3.1(预览版)和ef core 3将其保存到数据库中。获取数据没有问题,但是'context.SaveChanges()'抛出一个'算术溢出错误'异常:
'Arithmetic overflow error converting numeric to data type numeric.
The statement has been terminated.'
我已经尝试在模型中增加小数精度和小数位数,但是我仍然做错了什么,无法弄清它是什么。
型号:
public class PoloniexTradingPair
{
// this is the poloniex ID!
public int Id { get; set; }
[Key]
public int PoloniexTradingPairId { get; set; }
[Column(TypeName = "decimal(30,28)")]
public Decimal last { get; set; }
[Column(TypeName = "decimal(30,28)")]
public Decimal lowestAsk { get; set; }
[Column(TypeName = "decimal(30,28)")]
public Decimal highestBid { get; set; }
[Column(TypeName = "decimal(30,28)")]
public Decimal percentChange { get; set; }
[Column(TypeName = "decimal(30,28)")]
public Decimal baseVolume { get; set; }
[Column(TypeName = "decimal(38,20)")]
public Decimal quoteVolume { get; set; }
public int isFrozen { get; set; }
[Column(TypeName = "decimal(30,28)")]
public Decimal high24hr { get; set; }
[Column(TypeName = "decimal(30,28)")]
public Decimal low24hr { get; set; }
}
结果数据库表:
CREATE TABLE [dbo].[PoloniexTradingPairs](
[PoloniexTradingPairId] [int] IDENTITY(1,1) NOT NULL,[Id] [int] NOT NULL,[last] [decimal](30,28) NOT NULL,[lowestAsk] [decimal](30,[highestBid] [decimal](30,[percentChange] [decimal](30,[baseVolume] [decimal](30,[quoteVolume] [decimal](38,20) NOT NULL,[isFrozen] [int] NOT NULL,[high24hr] [decimal](30,[low24hr] [decimal](30,CONSTRAINT [PK_PoloniexTradingPairs] PRIMARY KEY CLUSTERED
(
[PoloniexTradingPairId] ASC
)WITH (PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
方法:
void getPoloniexTradingPairs()
{
using (ApplicationDbContext context = new ApplicationDbContext())
{
using (var httpClient = new HttpClient())
{
var rates = httpClient.GetStringAsync("https://poloniex.com/public?command=returnTicker");
JObject rates_json = JObject.Parse(rates.Result);
// Log.Information(rates_json.ToString());
foreach (var tradingpair in rates_json)
{
Log.Information(tradingpair.Key);
PoloniexTradingPair pair = JsonConvert.DeserializeObject<PoloniexTradingPair>(tradingpair.Value.ToString());
Log.Information("logging pair");
Log.Information(JsonConvert.SerializeObject(pair).ToString());
context.Add(pair);
Log.Information(pair.highestBid.ToString());
}
context.SaveChanges();
/*
JObject rates_json2 = JObject.Parse(rates_json.First.First.Last.First.ToString());
string rates_json3 = rates_json.First.First.First.Last.ToString();
CB_ExchangeRates ExchangeRates = JsonConvert.DeserializeObject<CB_ExchangeRates>(rates_json.First.First.Last.First.ToString());
ExchangeRates.currency = rates_json3;
context.Add(ExchangeRates);
context.SaveChanges();
Log.Information(JsonConvert.SerializeObject(ExchangeRates));
Log.Information("success!");
*/
/*
Log.Information("Pulling coinbase for exchange rates...");
var json = httpClient.GetStringAsync("https://api.coinbase.com/v2/prices/BTC-EUR/buy");
JObject jobject = JObject.Parse(json.Result);
CB_BTCEUR btceur = new CB_BTCEUR { Basecurrency = (string)jobject["data"]["base"],Targetcurrency = (string)jobject["data"]["currency"],Amount = (string)jobject["data"]["amount"] };
*/
}
}
}
运行时期间,配对看起来像预期的那样:
Id 7 int
PoloniexTradingPairId 0 int
baseVolume 0.70627038 decimal
high24hr 0.00000005 decimal
highestBid 0.00000004 decimal
isFrozen 0 int
last 0.00000004 decimal
low24hr 0.00000004 decimal
lowestAsk 0.00000005 decimal
percentChange 0.00000000 decimal
quoteVolume 17019678.54214583 decimal
我不知道如何找出哪个值真正导致了问题,并且除了“行号3”外,找不到任何异常信息。我假设它是指SQL查询,有什么方法可以查看查询吗? 该数据库似乎反映了我的模型,但是还是有问题。
感谢您提供解决此问题的任何提示! :)