Esempio n. 1
0
        public async Task <T> Add(T entity)
        {
            _context.Entry(entity).State = EntityState.Added;
            await _context.SaveChangesAsync();

            return(entity);
        }
Esempio n. 2
0
 public ActionResult DeleteOrderPosition(int id)
 {
     using (var context = new EfDbContext())
     {
         var orderId = context.OrderPositions.Find(id).OrderId;
         context.Entry(context.OrderPositions.Find(id)).State = EntityState.Deleted;
         context.SaveChanges();
         var order = context.Orders.Find(orderId);
         order.OrderPosition = context.OrderPositions.Where(o => o.OrderId == orderId).ToList();
         order.OrderValue    = 0;
         foreach (var element in order.OrderPosition)
         {
             order.OrderValue += element.Amount * element.PurchasePrice;
         }
         if (order.OrderValue <= 0)
         {
             context.Entry(order).State = EntityState.Deleted;
         }
         else
         {
             context.Entry(order).State = EntityState.Modified;
         }
         context.SaveChanges();
     }
     TempData["message"] = "Udało sie zapisać zmiany";
     return(RedirectToAction("Orders"));
 }
Esempio n. 3
0
        /// <summary>
        /// 变更追踪原理
        /// </summary>
        /// <param name="efContext"></param>
        public static void ChangeTracking(EfDbContext efContext)
        {
            //当我们将类中所有属性设置为virtual时,上下文中存在的就是POCO而是其派生类,此时实体总是被跟踪
            var entity = efContext.Set <Customer>().FirstOrDefault(d => d.Name == "ChenJie");

            Console.WriteLine(efContext.Entry(entity).State);
            entity.Name = "ChenJie";
            Console.WriteLine(efContext.Entry(entity).State);
        }
Esempio n. 4
0
        public void UpdateMode(AutomaticMode automaticMode)
        {
            context.Modes.Attach(automaticMode);
            var entry = context.Entry(automaticMode);

            entry.Property(x => x.Id).IsModified     = false;
            entry.Property(x => x.Status).IsModified = true;
            entry.Property(x => x.Time).IsModified   = true;
            context.SaveChanges();
        }
Esempio n. 5
0
        public Persone AllPersoneWithContact(int id, bool includeContact = false)
        {
            var existPersone = context.Persones.FirstOrDefault(x => x.Id == id);

            if (existPersone != null && includeContact)
            {
                context.Entry(existPersone).Collection(x => x.Contacts).Load();
            }
            return(existPersone);
        }
Esempio n. 6
0
        public async Task DeleteAsync(T entity)
        {
            if (_context.Entry(entity).State == EntityState.Detached)
            {
                _dbSet.Attach(entity);
            }

            _dbSet.Remove(entity);

            await _context.SaveChangesAsync();
        }
Esempio n. 7
0
        public Persone Persones(int personeId)
        {
            Persone p1 = context.Persones.FirstOrDefault(x => x.Id == personeId);

            if (p1 != null)
            {
                context.Entry(p1).Collection(x => x.Contacts).Load();
                return(p1);
            }
            return(null);
        }
 public ActionResult <string> Put(Guid id, [FromBody] Table table)
 {
     try
     {
         _context.Entry(table).State = EntityState.Modified;
         _context.SaveChanges();
         return(Ok("Created successfully"));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
Esempio n. 9
0
 public IActionResult Put(Guid id, [FromBody] Item item)
 {
     try
     {
         _context.Entry(item).State = EntityState.Modified;
         _context.SaveChanges();
         return(Ok("Created successfully"));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex));
     }
 }
Esempio n. 10
0
 public ActionResult AddCategory(Category category)
 {
     if (category.CategoryId > 0)
     {
         using (var context = new EfDbContext())
         {
             context.Entry(category).State = EntityState.Modified;
             context.SaveChanges();
         }
         TempData["message"] = "Udało się zaktualizować kategorię";
         return(RedirectToAction("Categories"));
     }
     else
     {
         if (ModelState.IsValid)
         {
             using (var context = new EfDbContext())
             {
                 context.Entry(category).State = EntityState.Added;
                 context.SaveChanges();
             }
             TempData["message"] = "Udało się dodać kategorię";
             return(RedirectToAction("Categories"));
         }
         else
         {
             return(View(category));
         }
     }
 }
Esempio n. 11
0
        /// <summary>
        /// Thực hiện nhập khẩu nhân viên
        /// </summary>
        /// <param name="keyImport">Key xác định lấy dữ liệu để nhập khẩu từ cache</param>
        /// <param name="overriderData">Có cho phép ghi đè hay không (true- ghi đè dữ liệu trùng lặp trong db)</param>
        /// <param name="cancellationToken">Tham số tùy chọn xử lý đa luồng (hiện tại chưa sử dụng)</param>
        /// <returns>ActionServiceResult(với các thông tin tương ứng tùy thuộc kết nhập khẩu)</returns>
        /// CreatedBy: NVMANH (10/10/2020)
        public override async Task <ActionServiceResult> Import(string importKey, bool overriderData, CancellationToken cancellationToken)
        {
            var employees = ((List <Employee>)CacheGet(importKey)).Where(e => e.ImportValidState == ImportValidState.Valid || (overriderData && e.ImportValidState == ImportValidState.DuplicateInDb)).ToList();;

            using var dbContext = new EfDbContext();

            // Danh sách các vị trí/ chức vụ mới:
            var newPositons = ((List <Position>)CacheGet(string.Format("Position_{0}", importKey)));
            await dbContext.Position.AddRangeAsync(newPositons);

            // Danh sách nhân viên thêm mới:
            var newEmployees = employees.Where(e => e.ImportValidState == Core.Enumeration.ImportValidState.Valid).ToList();
            await dbContext.Employee.AddRangeAsync(newEmployees);

            // Danh sách nhân viên thực hiện ghi đè:
            var modifiedEmployees = employees.Where(e => e.ImportValidState == Core.Enumeration.ImportValidState.DuplicateInDb).ToList();

            foreach (var emp in modifiedEmployees)
            {
                dbContext.Entry(emp).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                var pbd = dbContext.EmployeeFamily.Where(e => e.EmployeeId == emp.EmployeeId);
                dbContext.EmployeeFamily.AddRange(emp.EmployeeFamily);
                dbContext.EmployeeFamily.RemoveRange(pbd);
            }
            //dbContext.Employee.UpdateRange(modifiedEmployees);
            await dbContext.SaveChangesAsync();

            return(new ActionServiceResult(true, Resources.Msg_ImportSuccess, MISACode.Success, employees));
        }
Esempio n. 12
0
        public dynamic Save(Settings settings)
        {
            if (!ModelState.IsValid)
            {
                List <string> errors = new List <string>();
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    errors.AddRange(modelState.Errors.Select(er => er.ErrorMessage));
                }
                Response.StatusCode = 400;
                return(Json(errors));
            }

            if (db.Settings.Count() == 0)
            {
                db.Settings.Add(settings);
            }
            else
            {
                db.Entry(settings).State = System.Data.Entity.EntityState.Modified;
            }

            db.SaveChanges();
            return(Json(settings));
        }
Esempio n. 13
0
 public void EditProject(Project obj)
 {
     if (obj.Name != null && obj.Name.Equals(""))
     {
         ctx.Entry <Project>(obj).CurrentValues.SetValues(obj);
         ctx.SaveChanges();
     }
 }
 public void Update(Employee employee)
 {
     using (var dbContext = new EfDbContext())
     {
         dbContext.Entry(employee).State = EntityState.Modified;
         dbContext.SaveChanges();
     }
 }
Esempio n. 15
0
        /// <summary>
        /// Cập nhật dữ liệu cho đối tượng sử dụng EF Core
        /// </summary>
        /// <param name="entity">Đối tượng sẽ cập nhật</param>
        /// <returns></returns>
        /// CreatedBy: NVMANH (05/2020)
        public virtual async Task UpdateAsync(T entity)
        {
            using var dbContext = new EfDbContext();
            DoBeforeSave(entity);
            dbContext.Entry(entity).State = EntityState.Modified;
            await SaveChangesAsync();

            DoAfterInsert(entity);
        }
Esempio n. 16
0
 public ActionResult Edit([Bind(Include = "Id,StartDateTime")] Class @class)
 {
     if (ModelState.IsValid)
     {
         db.Entry(@class).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(@class));
 }
Esempio n. 17
0
 public ActionResult Edit([Bind(Include = "ClientId,Name,Surname,Address")] Client client)
 {
     if (ModelState.IsValid)
     {
         db.Entry(client).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(client));
 }
Esempio n. 18
0
 public ActionResult DeleteOrder(int id)
 {
     using (var context = new EfDbContext())
     {
         context.Entry(context.Orders.Find(id)).State = EntityState.Deleted;
         context.SaveChanges();
     }
     TempData["message"] = "Udało się usunąć zamówienie";
     return(RedirectToAction("Orders"));
 }
 public ActionResult Edit([Bind(Include = "hallId,typ")] Hall hall)
 {
     if (ModelState.IsValid)
     {
         db.Entry(hall).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(hall));
 }
Esempio n. 20
0
 public ActionResult Edit([Bind(Include = "NUmber,str,check,check2")] Test test)
 {
     if (ModelState.IsValid)
     {
         db.Entry(test).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(test));
 }
Esempio n. 21
0
 public ActionResult Edit([Bind(Include = "seanceId,start,length,movieId,hallId")] Seance seance)
 {
     if (ModelState.IsValid)
     {
         db.Entry(seance).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(seance));
 }
 public ActionResult Edit([Bind(Include = "seanceId,placesCount,placesDict,clientId,amount,paid")] Reservation reservation)
 {
     if (ModelState.IsValid)
     {
         db.Entry(reservation).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(reservation);
 }
Esempio n. 23
0
 public ActionResult Edit(Limit limit)
 {
     if (ModelState.IsValid)
     {
         db.Entry(limit).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(limit));
 }
 public ActionResult Edit([Bind(Include = "Id,Name")] Course course)
 {
     if (ModelState.IsValid)
     {
         db.Entry(course).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(course));
 }
 public ActionResult Edit([Bind(Include = "ID,Title,ReleaseDate,Genre,Price")] Movie movie)
 {
     if (ModelState.IsValid)
     {
         db.Entry(movie).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(movie));
 }
Esempio n. 26
0
 public ActionResult Edit([Bind(Include = "movieId,title,length")] Movie movie)
 {
     if (ModelState.IsValid)
     {
         db.Entry(movie).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(movie));
 }
Esempio n. 27
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,Name,AddressLine1,AddressLine2,City,State,Country,Pin")] Agency agency)
        {
            if (ModelState.IsValid)
            {
                db.Entry(agency).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(agency));
        }
        public async Task <ActionResult> Edit([Bind(Include = "ID,Name,Date,Amount,Comments,IsPrepared,IsVerified,IsPassed")] VehicleBill vehicleBill)
        {
            if (ModelState.IsValid)
            {
                db.Entry(vehicleBill).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(vehicleBill));
        }
        public async Task <ActionResult> Edit([Bind(Include = "ID,Title,ReleaseDate,Price")] DemoModel demoModel)
        {
            if (ModelState.IsValid)
            {
                db.Entry(demoModel).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(demoModel));
        }
Esempio n. 30
0
        /// <summary>
        /// 不使用Add方法去添加实体(added)
        /// </summary>
        /// <param name="efContext"></param>
        public static void ModelAdded(EfDbContext efContext)
        {
            var customer = new Customer
            {
                Name  = "chenjie",
                Email = "*****@*****.**"
            };

            efContext.Entry(customer).State = System.Data.Entity.EntityState.Added;
            efContext.SaveChanges();
        }