Exemplo n.º 1
0
        public static string Upsert <T>(T[] items) where T : EntityObject, IUserRecord
        {
            var db      = new x360ceModelContainer();
            var table   = db.CreateObjectSet <T>();
            var created = 0;
            var updated = 0;

            for (int i = 0; i < items.Length; i++)
            {
                var         item       = items[i];
                var         computerId = item.ComputerId;
                var         profileId  = item.ProfileId;
                IUserRecord dbItem     = GetExistingItem(table, item, true);
                // If database item was not found then...
                if (dbItem == null)
                {
                    // Use supplied item as new item.
                    created++;
                    item.DateCreated = DateTime.Now;
                    item.DateUpdated = item.DateCreated;
                    table.AddObject(item);
                }
                else
                {
                    updated++;
                    // Use created date from db.
                    item.DateCreated = dbItem.DateCreated;
                    item.DateUpdated = DateTime.Now;
                    RuntimeHelper.CopyDataMembers(item, dbItem, true);
                }
            }
            db.SaveChanges();
            db.Dispose();
            return(string.Format("{0}s: {1} created, {2} updated.", items.GetType().GetElementType().Name, created, updated));
        }