示例#1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public int InsertPost(string request)
        {
            //string auxRequest = "{\"idInvoice\":12,\"Number\":1,\"Concept\":\"235.61.278\",\"Description\":\"Payment Kantoor Utrecht\",\"total\":1345,\"dateI\":\"\\/Date(1443280786427+0200)\\/\",\"dateF\":\"\\/Date(1447421394063+0100)\\/\",\"InvoiceLines\":[{\"idLine\":4,\"rIdInvoice\":12,\"sDesc\":\"Line 1\",\"total\":1345}]}";
            //JToken jsonRequest = JToken.Parse(auxRequest);

            JToken jsonRequest = JToken.Parse(request);

            using (InvoiceClassesDataContext dc = new InvoiceClassesDataContext())
            {
                int idInvoice = -1;

                Invoice i = new Invoice();
                i.Number = Int32.Parse(jsonRequest["Number"].ToString());
                i.Concept = jsonRequest["Concept"].ToString();
                i.Description = jsonRequest["Description"].ToString();
                i.total = Int32.Parse(jsonRequest["total"].ToString());
                i.dateI = DateTime.Parse(jsonRequest["dateI"].ToString());
                i.dateF = DateTime.Parse(jsonRequest["dateF"].ToString());
                dc.Invoices.InsertOnSubmit(i);
                dc.SubmitChanges();
                idInvoice = i.idInvoice;

                JArray lines = JArray.Parse(jsonRequest["InvoiceLines"].ToString());
                foreach (var line in lines)
                {
                    InvoiceLine iLine = new InvoiceLine();
                    iLine.rIdInvoice = idInvoice;
                    iLine.sDesc = line["sDesc"].ToString();
                    iLine.total = Int32.Parse(line["total"].ToString());
                    dc.InvoiceLines.InsertOnSubmit(iLine);
                }

                try
                {
                    dc.SubmitChanges();
                    return idInvoice;
                }
                catch (Exception ex)
                {
                    bool bDelete = Delete(idInvoice.ToString());
                    return -1;
                }
            }
        }
 partial void DeleteInvoiceLine(InvoiceLine instance);
 partial void UpdateInvoiceLine(InvoiceLine instance);
 partial void InsertInvoiceLine(InvoiceLine instance);
		private void detach_InvoiceLines(InvoiceLine entity)
		{
			this.SendPropertyChanging();
			entity.Invoice = null;
		}
		private void attach_InvoiceLines(InvoiceLine entity)
		{
			this.SendPropertyChanging();
			entity.Invoice = this;
		}