我在Web API ASP.NET中有一个post方法,它可以接受一个Json对象.这是带有代码的代码,用于检查我的数据库中是否存在来自发布Json的数据.我将使用Linq与我的数据库进行交互.我相信EntityFramework是在我的上下文中自动生成Sales_的. db是我的数据库对象.
I have a post method in Web API ASP.NET that accepts a single Json object. Here is the code with a code to check if data from the posting Json is existing in my database. and I will be using Linq to interact with my DB. Sales_ was auto-generated in my context, I believe from EntityFramework. db is my database object.
public HttpResponseMessage PostSales(Sales Sales, [FromUri] string auth) { try { if (ModelState.IsValid) { if (auth == "KDI") { Int64 rs = db.Sales_.Where(sl => sl.Serial == Sales.Serial).Count(); if (rs == 1) { return Request.CreateErrorResponse(HttpStatusCode.Conflict, " Duplicate Found!"); } else { db.Sales_.Add(Sales); db.SaveChanges(); return Request.CreateErrorResponse(HttpStatusCode.OK, "Added!"); } //Closing elses cut out for this question.这很好,它接受我的Json罚款作为单个对象.
That is working fine and it accepts my Json fine as a single object.
这是我的杰森(Json),将被发布.
Here is my Json that will be posted.
{ "Serial": 1, "ExtSerial": "AH0000002", "Date": "2015-03-01", "CustomerRefNbr": "JPM0001", "Description": "2015 FEBRUARY RENTAL 2015 FEBRUARY RENTAL", "Customer": "TRDE0065", "Amount": 17989.51, "AQ_Branch": "KDI", "AQ_COA": "4100503000", "LineSubAccount": "OPMOPN000", "LineTaxCategory": "JPMTAX", "LineQuantity": 1, "LineUnitPrice": 400000, "AQ_PostStatus": 1, "AQ_StatusDate": "2015-03-01", "DTS": "2015-03-01", "LineDescription": "Line Description" }现在,我想在json对象中发布多个列表或行(不确定如何称呼它们).像这样:
Now, I wanted to post multiple lists or lines (not sure what to call them) in a json object. Something like this:
{ "ExtSerial": "AH0000002", "Date": "2015-03-01", "CustomerRefNbr": "JPM0001", "Description": "2015 FEBRUARY RENTAL 2015 FEBRUARY RENTAL", "Customer": "TRDE0065", "Amount": 17989.51, "AQ_Branch": "KDI", "AQ_COA": "4100503000", "LineSubAccount": "OPMOPN000", "LineTaxCategory": "JPMTAX", "LineQuantity": 1, "LineUnitPrice": 400000, "AQ_PostStatus": 1, "AQ_StatusDate": "2015-03-01", "DTS": "2015-03-01", "LineDescription": "Line Description" },{ "ExtSerial": "AH0000003", "Date": "2015-04-01", "CustomerRefNbr": "JPM0002", "Description": "2015 FEBRUARY RENTAL 2015 FEBRUARY RENTAL", "Customer": "TRDE0066", " Amount": 17989.51, "AQ_Branch": "KDI", "AQ_COA": "4100503000", "LineSubAccount": "OPMOPN000", "LineTaxCategory": "JPMTAX", "LineQuantity": 2, "LineUnitPrice": 400000, "AQ_PostStatus": 1, "AQ_StatusDate": "2015-04-01", "DTS": "2015-04-01", "LineDescription": "Line Description" }虽然不确定如何在我的post方法上读取它.我尝试将参数Sales更改为List<Sales> Sales.我认为这是用于对象列表的方法,但是我的问题是我的db.Sales_.Add(Sales);现在产生错误. (无效的参数)不确定如何将.Add()与列表一起使用.另外,我无法获得Int64 rs = db.Sales_.Where(sl => sl.Serial == Sales.Serial).Count();
Not sure how it will be read on my post method though. I have tried changing my parameter Sales to List<Sales> Sales. I think that is what to use for a list in an object but my problem is that my db.Sales_.Add(Sales); is producing an error now. (invalid arguments) Not sure how to use the .Add() with a list. Also, I can't get the data of my Sales table column serial as shown in Int64 rs = db.Sales_.Where(sl => sl.Serial == Sales.Serial).Count();
我很困,不知道该怎么做.希望您不要觉得我的问题很愚蠢.我只需要你的帮助.我知道这很简单.
I'm pretty much stuck and don't know how to do this. I hope you don't find my question stupid. I just need your help. I know it's a simple fix.
推荐答案JSON数组包装在[]中,而且您的Sales参数必须是列表或数组.
JSON arrays are wrapped in [], also your Sales argument must be of list or array.
将方法签名更改为
public HttpResponseMessage PostSales(List<Sales> Sales, [FromUri] string auth)将您的json更改为以[]包裹,例如
Change your json to be wrapped in [] like
[{ "ExtSerial": "AH0000002", "Date": "2015-03-01", "CustomerRefNbr": "JPM0001", "Description": "2015 FEBRUARY RENTAL 2015 FEBRUARY RENTAL", "Customer": "TRDE0065", "Amount": 17989.51, "AQ_Branch": "KDI", "AQ_COA": "4100503000", "LineSubAccount": "OPMOPN000", "LineTaxCategory": "JPMTAX", "LineQuantity": 1, "LineUnitPrice": 400000, "AQ_PostStatus": 1, "AQ_StatusDate": "2015-03-01", "DTS": "2015-03-01", "LineDescription": "Line Description" },{ "ExtSerial": "AH0000003", "Date": "2015-04-01", "CustomerRefNbr": "JPM0002", "Description": "2015 FEBRUARY RENTAL 2015 FEBRUARY RENTAL", "Customer": "TRDE0066", " Amount": 17989.51, "AQ_Branch": "KDI", "AQ_COA": "4100503000", "LineSubAccount": "OPMOPN000", "LineTaxCategory": "JPMTAX", "LineQuantity": 2, "LineUnitPrice": 400000, "AQ_PostStatus": 1, "AQ_StatusDate": "2015-04-01", "DTS": "2015-04-01", "LineDescription": "Line Description" }]现在,您只需枚举Sales并分别处理每个.像
Now you just enumerate through your Sales and process each one individually. Something like
Parallel.ForEach(Sales, sale => { //you can clean this up using Any(), instead of Where.. //Int64 rs = db.Sales_.Where(sl => sl.Serial == sale.Serial).Count(); using (var dbContext = new YourDatabaseContext()) { if (dbContext .Sales_.Any(sl => sl.Serial == sale.Serial)) { return; //ignore duplicates } else { dbContext .Sales_.Add(sale); dbContext .SaveChanges(); } } }); return Request.CreateErrorResponse(HttpStatusCode.OK, "Finished"); //you can just return OK considering a duplicate will not break the client.更多推荐
如何获取将发布到Web API的多个json对象列表的名称数据?
发布评论