这是我的问题的延续:[How to get name data of multiple json object list that will be posted to web api?
我能够更新我的代码,但现在我在
上得到了一个erexceptionif (db.Sales1.Any(sl => sl.ExtSerial != s.ExtSerial))
异常:EntityFramework.dll中发生了'System.InvalidOperationException'类型的异常,但未在用户代码中处理
附加信息:创建模型时无法使用上下文。如果在OnModelCreating方法中使用上下文,或者同时由多个线程访问相同的上下文实例,则可能抛出此异常。请注意,DbContext和相关类的实例成员不保证是线程安全的。
以下是代码:
public HttpResponseMessage PostSales(List<Sales> Sales, [FromUri] string auth)
{
try
{
if (ModelState.IsValid)
{
if (auth == "KDI")
{
#region Stable but not multiline
//Int64 rs = db.Sales1.Where(sl => sl.Serial == Sales.Serial).Count();
//if (1 == rs)
//{
// return Request.CreateErrorResponse(HttpStatusCode.Conflict, " Duplicate Found!");
//}
//else
//{
// db.Sales1.Add(Sales);
// db.SaveChanges();
// return Request.CreateErrorResponse(HttpStatusCode.OK, "Added!");
//}
#endregion
Parallel.ForEach(Sales, s =>
{
if (db.Sales1.Any(sl => sl.ExtSerial != s.ExtSerial))
{
db.Sales1.Add(s);
db.SaveChanges();
}
else
{
return;
}
});
return Request.CreateErrorResponse(HttpStatusCode.OK, "Success!");
}
else
{
return Request.CreateResponse(HttpStatusCode.Unauthorized, "Unauthorized Access!");
}
}
else
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's wrong with the JSON model you sent me.");
}
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}