Exemplo n.º 1
0
 // POCO Output binding
 //   The binding inserts the newly created item into the specified Collection when the
 //   method successfully exits.
 //   If the database or the collection do not exist, they are automatically created.
 //
 // Other supported types:
 //   out T[]
 //   IAsyncCollector<T>
 //   ICollector<T>
 public static void InsertDocument(
     [TimerTrigger("00:01")] TimerInfo timer,
     [DocumentDB("ItemDb", "ItemCollection")] out ItemDoc newItem)
 {
     newItem = new ItemDoc()
     {
         Text = new Random().Next().ToString()
     };
 }
 // POCO Output binding
 //   The binding inserts the newly created item into the specified Collection when the
 //   method successfully exits.
 //   If the database or the collection do not exist, they are automatically created.
 //
 // Other supported types:
 //   out T[]
 //   IAsyncCollector<T>
 //   ICollector<T>
 public static void InsertDocument(
     [TimerTrigger("00:01")] TimerInfo timer,
     [CosmosDB("ItemDb", "ItemCollection", CreateIfNotExists = true)] out ItemDoc newItem)
 {
     newItem = new ItemDoc()
     {
         Text        = new Random().Next().ToString(),
         IsCompleted = new Random().Next() % 2 == 0
     };
 }
Exemplo n.º 3
0
        public bool UpdateItemWithDoc(ItemMt updateItem, ItemDoc itemDoc, bool newVersion = false)
        {
            try
            {
                if (updateItem == null)
                {
                    throw new ArgumentNullException(nameof(updateItem));
                }

                if (itemDoc == null)
                {
                    throw new ArgumentNullException(nameof(itemDoc));
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            ItemMt itemToUpdate = GetItem(updateItem.IdItemBcn);

                            if (itemToUpdate == null)
                            {
                                return(false);
                            }

                            //Hay que agregarlo al contexto actual para que lo actualice
                            db.ItemsMt.Attach(itemToUpdate);

                            itemToUpdate.IdSubVer += 1;
                            if (newVersion == true)
                            {
                                itemToUpdate.IdVer   += 1;
                                itemToUpdate.IdSubVer = 0;
                            }
                            itemToUpdate.Timestamp = DateTime.Now;

                            //no hacemos  un "entry" en el contexto de db y lo marcamos como modificado, sino que updatamos sólo los pocos campos
                            //que se pueden modificar de un item desde la aplicación para que EF genere el update sólo de esos campos y no de todos
                            itemToUpdate.IdDefaultSupplier = updateItem.IdDefaultSupplier;
                            itemToUpdate.IdFamilyHK        = updateItem.IdFamilyHK;
                            itemToUpdate.IdStatusProd      = updateItem.IdStatusProd;
                            itemToUpdate.IdUserAttri1      = updateItem.IdUserAttri1;
                            itemToUpdate.IdUserAttri2      = updateItem.IdUserAttri2;
                            itemToUpdate.IdUserAttri3      = updateItem.IdUserAttri3;
                            itemToUpdate.PhotoUrl          = updateItem.PhotoUrl;

                            ItemMtHistory itemHistory = (ItemMtHistory)itemToUpdate;
                            itemHistory.User = GlobalSetting.LoggedUser.UserLogin;

                            //Documento
                            itemDoc.IdVerItem    = itemToUpdate.IdVer;
                            itemDoc.IdSubVerItem = itemToUpdate.IdSubVer;
                            itemDoc.CreateDate   = DateTime.Now;

                            db.ItemsMtHistory.Add(itemHistory);
                            db.ItemsDoc.Add(itemDoc);

                            db.SaveChanges();
                            dbTrans.Commit();
                            return(true);
                        }
                        catch (DbEntityValidationException e)
                        {
                            dbTrans.Rollback();
                            _log.Error(e.Message, e);
                            throw e;
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
        }