Example #1
0
 public virtual void Delete(TEntity entityTODelete)
 {
     try
     {
         if (context.Entry(entityTODelete).State == EntityState.Deleted)
         {
             dbSet.Attach(entityTODelete);
         }
         dbSet.Remove(entityTODelete);
     }
     catch (System.Data.Entity.Core.EntityCommandExecutionException ex)
     {
         Delete(entityTODelete);
     }
 }
Example #2
0
        public async Task <ActionResult> Edit([Bind(Include = "standard_id,standard_name")] Standard standard)
        {
            if (ModelState.IsValid)
            {
                db.Entry(standard).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(standard));
        }
Example #3
0
        public async Task <ActionResult> Edit([Bind(Include = "student_id,student_name,standard_id")] Student student)
        {
            if (ModelState.IsValid)
            {
                db.Entry(student).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            ViewBag.standard_id = new SelectList(db.standard, "standard_id", "standard_name", student.standard_id);
            return(View(student));
        }
Example #4
0
        public async Task <IActionResult> PutLocal(long id, LocalModel item)
        {
            if (id != item.Id)
            {
                return(BadRequest());
            }

            _context.Entry(item).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(NoContent());
        }
        public async Task <IActionResult> Repair(RepairModel model)
        {
            var endpointDevice = await _context.EndpointDevices
                                 .FirstOrDefaultAsync(m => m.Identifier == model.DeviceId);

            if (endpointDevice == null)
            {
                return(NotFound("Nie znaleziono urządzenia końcowego"));
            }
            if (endpointDevice.TestResult == TestResult.Pozytywny)
            {
                return(BadRequest("Urządzenie końcowe zawiera pozytywny wynik testu"));
            }
            endpointDevice.TestResult            = TestResult.Pozytywny;
            _context.Entry(endpointDevice).State = EntityState.Modified;

            foreach (var component in model.ComponentModels)
            {
                var foundComponent = await _context.Components.FirstOrDefaultAsync(com =>
                                                                                   com.Identifier == component.ComponentIdentificator);

                if (foundComponent == null)
                {
                    return(BadRequest("Nie znaleziono komponentu"));
                }
                if (foundComponent.AvailableAmount < component.Amount)
                {
                    return(BadRequest("Brak wystarczającej ilości dostepnych komponentów"));
                }
                foundComponent.AvailableAmount -= component.Amount;

                _context.Entry(foundComponent).State = EntityState.Modified;
            }

            if ((await _context.SaveChangesAsync()) >= 0)
            {
                return(Ok());
            }
            return(StatusCode(500));
        }
Example #6
0
        public void HookedDbContext_MustCallHooks_IfModelIsInvalidButUnchanged()
        {
            var context = new LocalContext();

            context.RegisterHook(new TimestampPreInsertHook());
            var tsEntity  = new TimestampedSoftDeletedEntity();
            var valEntity = new ValidatedEntity();

            context.Entities.Add(tsEntity);
            context.Entry(valEntity).State = EntityState.Unchanged;

            Assert.DoesNotThrow(() => context.SaveChanges());

            Assert.AreEqual(tsEntity.CreatedAt.Date, DateTime.Today);
        }
        public virtual T AddOrUpdate(T t, object key)
        {
            if (t == null)
            {
                return(null);
            }
            T exist = _context.Set <T>().Find(key);

            if (exist != null)
            {
                _context.Entry(exist).CurrentValues.SetValues(t);
                _context.SaveChanges();
                return(exist);
            }
            else
            {
                _context.Set <T>().Add(t);
                _context.SaveChanges();
                return(t);
            }
        }