コード例 #1
0
        public string PostObjectsInTransaction([FromBody] string data)
        {
            StringBuilder returnValue = new StringBuilder();
            List <Tuple <KeyValuePair <string, List <string> >, string> > upsertedDataList = new List <Tuple <KeyValuePair <string, List <string> >, string> >();

            var entitiesToPersisit = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(data);

            //Start db operation
            var connWithTran = DataServices.BeginDbTransaction();

            try
            {
                entitiesToPersisit.Each(entityItems =>
                {
                    entityItems.Each(entity =>
                    {
                        var savedEntityWithIds = PersistMultipleOps(entity, connWithTran, returnValue);

                        if (savedEntityWithIds.Key != null && savedEntityWithIds.Key.IsNotEmpty()) // Do not allow without key
                        {
                            upsertedDataList.Add(new Tuple <KeyValuePair <string, List <string> >, string>(savedEntityWithIds, entity.Value.GetType().Name));
                        }
                    });
                });

                DataServices.CommitDbTransaction(connWithTran.Item1, connWithTran.Item2);
            }
            catch (Exception ex)
            {
                DataServices.RollbackDbTransaction(connWithTran.Item1, connWithTran.Item2);

                throw ex;
            }

            //Fetch upserted records from db - can be done once commit
            FetchUpsertedData(upsertedDataList, returnValue);

            return("[" + returnValue.ToString().TrimEnd(",") + "]");
        }