コード例 #1
0
        public void PartialUpdate(T value, List <string> properties)
        {
            var dbEntityEntry = DatabaseContext.Entry(value);

            foreach (var property in properties)
            {
                dbEntityEntry.Property(property).IsModified = true;
            }
        }
コード例 #2
0
        public void Delete(TEntity deletedEntity)
        {
            if (_context.Entry(deletedEntity).State == EntityState.Detached)
            {
                _context.Set <TEntity>().Attach(deletedEntity);
            }

            _context.Set <TEntity>().Remove(deletedEntity);
        }
コード例 #3
0
        public void DBContextEntryLoad()
        {
            using (var context = new NorthwindContext())
            {
                var employeeTerritories = context.EmployeeTerritories.First();

                context.Entry(employeeTerritories).Reference(a => a.Employee).Load();
                context.Entry(employeeTerritories).Reference(a => a.Territory).Query().Load();
            }
        }
コード例 #4
0
        public void Update(TEntity entity)
        {
            _context.Entry(entity).State = EntityState.Modified;

            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }
        }
コード例 #5
0
        public async Task <JsonResult> UpdateData([System.Web.Http.FromBody] OrderDetailProductViewModel orderDetailProductViewModel)
        {
            if (ModelState.IsValid)
            {
                int orderid   = orderDetailProductViewModel.OrderDetail.OrderID;
                int productid = orderDetailProductViewModel.Product.ProductID;
                db.Entry(orderDetailProductViewModel.OrderDetail).State = EntityState.Modified;
                db.Entry(orderDetailProductViewModel.Product).State     = EntityState.Modified;
                await db.SaveChangesAsync();

                return(Json(new { Result = "Save OK" }));
            }
            return(Json(new { Result = "Save Fail" }));
        }
コード例 #6
0
        public async Task <IActionResult> PutCustomer(string id, Customer customer)
        {
            if (id != customer.CustomerId)
            {
                return(BadRequest());
            }

            _context.Entry(customer).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #7
0
        public static bool UpdateTicketType(TicketType Type)
        {
            bool result = false;

            using (var ctx = new NorthwindContext())
            {
                using (var dbContextTransaction = ctx.Database.BeginTransaction())
                {
                    try
                    {
                        TicketType Query = (from t in ctx.TicketTypes where Type.Id == t.Id select t).FirstOrDefault();
                        ctx.Entry(Query).CurrentValues.SetValues(Type);
                        ctx.SaveChanges();
                        dbContextTransaction.Commit();
                        result = true;
                    }
                    catch (Exception)
                    {
                        dbContextTransaction.Rollback();
                        result = false;
                    }
                }
            }

            return(result);
        }
コード例 #8
0
        public async Task <IActionResult> PutProduct([FromRoute] int id, [FromBody] Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != product.ProductId)
            {
                return(BadRequest());
            }

            _context.Entry(product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #9
0
        //delete
        //delete (physical) or change (logical update) the entire entity record
        //the value returned is the number of rows affected
        public int Products_Delete(int productid)
        {
            using (var context = new NorthwindContext())
            {
                //Physical Delete
                //the physical removal of a record from the database

                ////locate the instance of the entity to be removed
                //var existing = context.Products.Find(productid);
                ////is it there
                //if (existing == null)
                //{
                //    throw new Exception("Record has been remove already.");
                //}
                ////Stage
                //context.Products.Remove(existing);
                ////commit
                //return context.SaveChanges();

                //Logical delete
                //you normal set a property to a specific value to
                //    indicate the record should be considered gone
                //this is actually an update of the record

                //locate the instance of the entity to be removed
                var existing = context.Products.Find(productid);
                //set the property to the specific value
                existing.Discontinued = true;
                //Stage
                context.Entry(existing).State = System.Data.Entity.EntityState.Modified;
                //commit
                return(context.SaveChanges());
            }
        }
コード例 #10
0
        public async Task <IActionResult> PutOrderDetail([FromRoute] string id, [FromBody] OrderDetail orderDetail)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != orderDetail.Id)
            {
                return(BadRequest());
            }

            _context.Entry(orderDetail).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrderDetailExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #11
0
        //this method deletes a record from the database
        //   or
        //this method logically flags a record to be deemed deleted from the database
        //this method returns the number of records affected on the database
        public int Products_Delete(int productid)
        {
            //start transaction
            using (var context = new NorthwindContext())
            {
                ////physical delete
                //var existing = context.Products.Find(productid);
                ////stage
                //context.Products.Remove(existing);
                ////commit
                //return context.SaveChanges();

                //logical delete
                var existing = context.Products.Find(productid);
                //alter the data value on the record that will
                //   logically deem the deleted deleted
                //You should NOT rely on the user to do this
                //   alternation on the web form
                existing.Discontinued = true;
                //stage
                context.Entry(existing).State = System.Data.Entity.EntityState.Modified;
                //commit
                return(context.SaveChanges());
            }

            //to query your database using a non primary key value
            //this will require a sql procedure to call
            //the namespace System.Data.SqlClient is required
            //the returning datatype is IEnumerable<T>
            //this returning datatype will be cast using ToList() on the return
        }
コード例 #12
0
        public async Task <IActionResult> PutCategory(int id, [FromBody] Category category)
        {
            if (id != category.CategoryId)
            {
                return(BadRequest());
            }

            _context.Entry(category).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #13
0
        public async Task <IActionResult> PutTerritory([FromRoute] string id, [FromBody] Territory territory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != territory.TerritoryId)
            {
                return(BadRequest());
            }

            _context.Entry(territory).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TerritoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #14
0
        public async Task <IActionResult> PutOrders(int id, Orders orders)
        {
            if (id != orders.OrderId)
            {
                return(BadRequest());
            }

            _context.Entry(orders).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrdersExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #15
0
        public static bool UpdateClient(Client Client)
        {
            bool result = false;

            using (var ctx = new NorthwindContext())
            {
                using (var dbContextTransaction = ctx.Database.BeginTransaction())
                {
                    try
                    {
                        Client query = (from x in ctx.Clients where x.Id == Client.Id select x).FirstOrDefault();
                        ctx.Entry(query).CurrentValues.SetValues(Client);
                        ctx.SaveChanges();
                        dbContextTransaction.Commit();
                        result = true;
                    }
                    catch (Exception)
                    {
                        dbContextTransaction.Rollback();
                        result = false;
                    }
                }
            }
            return(result);
        }
コード例 #16
0
        public async Task <IHttpActionResult> PutCategory(int id, Category category)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != category.CategoryID)
            {
                return(BadRequest());
            }

            db.Entry(category).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #17
0
        public async Task <ActionResult> PutSupplier(int id, [FromBody] Supplier editedSupplier)
        {
            if (id == editedSupplier.SupplierId)
            {
                return(BadRequest());
            }

            northwindContext.Entry(editedSupplier).State = EntityState.Modified;

            try
            {
                await northwindContext.SaveChangesAsync();
            }
            catch (DBConcurrencyException)
            {
                var isSupplierExist = northwindContext.Suppliers.Any(a => a.SupplierId == id);

                if (!isSupplierExist)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #18
0
        /// <summary>
        /// Get entity by primary key
        /// </summary>
        /// <param name="id">primary key to find</param>
        /// <param name="references">empty, one or more navigation property by name</param>
        /// <returns>Entity if found along with navigation items if specified</returns>
        public async Task <TEntity> GetTask(int id, string[] references = null)
        {
            var model = await _dbSet.FindAsync(id);

            if (references == null)
            {
                return(model);
            }

            foreach (var reference in references)
            {
                _context.Entry((object)model).Reference(reference).Load();
            }

            return(model);
        }
コード例 #19
0
        public async Task <ActionResult> PutEmployee(int id, [FromBody] Employee editedEmployee)
        {
            if (id != editedEmployee.EmployeeId)
            {
                return(BadRequest());
            }

            northwindContext.Entry(editedEmployee).State = EntityState.Modified;

            try
            {
                await northwindContext.SaveChangesAsync();
            }
            catch (DBConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok());
        }
コード例 #20
0
        public async Task <IHttpActionResult> PutEmployee(int id, Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != employee.EmployeeID)
            {
                return(BadRequest());
            }

            db.Entry(employee).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return(NotFound());
                }
                throw;
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #21
0
        public IHttpActionResult PutCustomer(string id, Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customer.CustomerID)
            {
                return(BadRequest());
            }

            db.Entry(customer).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #22
0
        public async Task <IActionResult> PutRegion(int id, Region region)
        {
            if (id != region.Id)
            {
                return(BadRequest());
            }

            _context.Entry(region).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RegionExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #23
0
        //Delete
        //physical delete: physical removal of the record from the database
        //logical delete: usually some record attribute is set to indicate
        //                that this record should be ignored
        //input: the pkey value of the record
        //output: rows affected
        public int Product_Delete(int productid)
        {
            //transaction
            using (var context = new NorthwindContext())
            {
                //physical
                //  removal of record from the database

                ////find and retain the record to remove
                //var existing = context.Products.Find(productid);
                ////stage record for removal
                //context.Products.Remove(existing);
                ////commit
                //return context.SaveChanges();

                //logical
                //  this action will actually be an Update
                //  any attributes that are required for tracking
                //      need to be handled
                //  the attribute that indicates the record is logically
                //      removed needs to be handled

                //find record to be "deleted"
                var existing = context.Products.Find(productid);
                //adjust logical/tracking attributes
                //existing.LastModified = DateTime.Now;
                existing.Discontinued = true;
                //stage for update
                context.Entry(existing).State = System.Data.Entity.EntityState.Modified;
                //commit
                return(context.SaveChanges());
            }
        }
コード例 #24
0
        public async Task <ActionResult <Product> > PutProduct(int id, [FromBody] Product product)
        {
            if (id != product.ProductId)
            {
                return(BadRequest());
            }

            northwindContext.Entry(product).State = EntityState.Modified;

            try
            {
                await northwindContext.SaveChangesAsync();
            }
            catch (DBConcurrencyException)
            {
                if (!ProductExisit(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(product);
        }
コード例 #25
0
        public async Task <IActionResult> PutEmployees([FromRoute] int id, [FromBody] Employees employees)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != employees.EmployeeID)
            {
                return(BadRequest());
            }

            _context.Entry(employees).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #26
0
        //Update
        //the update will receive an instance <T>
        //the instance will have the pkey value
        //the commit will return the number of rows affected
        public int Product_Update(Product item)
        {
            using (var context = new NorthwindContext())
            {
                //optional:
                //there could be an attribute(s) on your table
                //    that track alternations to the database
                //    record such as date of change, security id
                //    of person making the change
                //let us assume that there is an attribute on
                //    the record which records the update date
                //item.LastModified = DateTime.Now;


                //staging
                //this update approach will update the entire
                //    record on the data that matches
                //    the pkey value of the instance
                context.Entry(item).State = System.Data.Entity.EntityState.Modified;

                //commit
                //the execution returns the number of rows affected
                return(context.SaveChanges());
            }
        }
コード例 #27
0
        //this method deletes a record from the database
        //      or
        //the method logically flags a record to be deemed deleted forn the databse
        //this method reutrns the number of records affected on the database
        public int Products_Delete(int productid)
        {
            //start transaction
            using (var context = new NorthwindContext())
            {
                //physical delete
                //var existing = context.Products.Find(productid);
                //stage
                //context.Products.Remove(existing);
                //commit
                //return context.SaveChanges();

                //logical
                var existing = context.Products.Find(productid);
                //alter the data balue on the record that will
                //      logically deem the deketed deketed
                //You should NOT rely on the user to do this
                //      alternation on the web form
                existing.Discontinued = true;
                //stage
                context.Entry(existing).State = System.Data.Entity.EntityState.Modified;
                //commit
                return(context.SaveChanges());
            }
        }
コード例 #28
0
        //Delete
        //physical: will remove the record physically from the database
        //logical: will usually set an attribute on the database record
        //         indicating that the record should be ignored in
        //         normal processing
        //input: only the pkey value is required
        //output: number of rows affected
        public int Product_Delete(int productid)
        {
            using (var context = new NorthwindContext())
            {
                //physical
                ////find record on database to delete
                //var existing = context.Products.Find(productid);
                ////remove the physical instance (staging)
                //context.Products.Remove(existing);
                ////commit
                //return context.SaveChanges();

                //logical
                //find record on database to "delete"
                var existing = context.Products.Find(productid);

                //staging
                //the attribute used to flag the records
                //    as a logical delete SHOULD be set by
                //    the controller method and NOT be relied
                //    upon by the used
                existing.Discontinued = true;
                //existing.LastModied = DateTime.Now; as in update
                //the staging for a logical delete is actually an
                //   an update
                context.Entry(existing).State = System.Data.Entity.EntityState.Modified;
                //commit
                return(context.SaveChanges());
            }
        }
コード例 #29
0
        public async Task <IActionResult> PutCustomers([FromRoute] string id, [FromBody] Customers customers)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customers.CustomerID)
            {
                return(BadRequest());
            }

            _context.Entry(customers).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomersExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #30
0
        public async Task <IActionResult> PutProducts(int id, Products products)
        {
            var context = new NorthwindContext();

            if (id != products.ProductId)
            {
                return(BadRequest());
            }

            context.Entry(products).State = EntityState.Modified;

            try
            {
                context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }