コード例 #1
0
        private static T InsertAudit(SampleContext sampleContext, BaseEntity <T> entity, AuditEntities.Enums.AuditType auditTypeId)
        {
            var audit      = (T)Activator.CreateInstance(typeof(T));
            var auditType  = audit.GetType();
            var entityType = entity.GetType();

            var entityProps = entityType.GetProperties()
                              .Where(x => IsSimpleType(x.PropertyType))
                              .ToDictionary(x => x.Name, x => x);

            var auditProps = auditType.GetProperties()
                             .Where(x => entityProps.ContainsKey(x.Name))
                             .ToDictionary(x => x.Name, x => x);

            var props = new string[auditProps.Count];

            auditProps.Keys.CopyTo(props, 0);
            foreach (var prop in props)
            {
                var value = entityProps[prop].GetValue(entity);
                auditProps[prop].SetValue(audit, value);
            }

            audit.AuditDate   = DateTime.UtcNow;
            audit.AuditID     = Guid.NewGuid();
            audit.AuditTypeID = auditTypeId;
            audit.SessionID   = sampleContext.SessionID;

            sampleContext.Add(audit);
            return(audit);
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("Id,Title,ReleaseDate,Genre,Price")] SampleModel sampleModel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(sampleModel);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(sampleModel));
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("Id,EmpCompanyname,Ecmplocation,Esalary")] Empcompnaydetails empcompnaydetails)
        {
            if (ModelState.IsValid)
            {
                _context.Add(empcompnaydetails);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(empcompnaydetails));
        }
コード例 #4
0
ファイル: TeachersController.cs プロジェクト: noegraha/Entity
        public async Task <IActionResult> Create([Bind("Id,Name,Skills,TotalStudents,Salary,AddedOn,Address,NoHp,Tanggal")] Teacher teacher)
        {
            if (ModelState.IsValid)
            {
                _context.Add(teacher);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(teacher));
        }
コード例 #5
0
        public async Task <IActionResult> Create([Bind("UserId,UserName,Password,IsAdmin")] User user)
        {
            if (ModelState.IsValid)
            {
                user.UserId = Guid.NewGuid();
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
コード例 #6
0
        public void Test1()
        {
            var options = new DbContextOptionsBuilder <SampleContext>().Options;

            using (var context = new SampleContext(options))
            {
                context.Add(new Country()
                {
                    Id = Guid.NewGuid(), Name = "Portugal"
                });
            }
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: fatihkck/RabbitMQ.Sample
        public static void TableSave(Guid uniqueId, int rowNo, string name, string description)
        {
            using (SampleContext db = new SampleContext())
            {
                Person person = new Person();
                person.UniqueId    = uniqueId;
                person.RowNo       = rowNo;
                person.Name        = name;
                person.Description = description;

                db.Add(person);
                db.SaveChanges();
            }
        }
コード例 #8
0
        public async Task CreateRequestForCommandAsync <T>(Guid id)
        {
            var exists = await ExistAsync(id);

            var request = exists ?
                          throw new SampleDomainException($"Request with {id} already exists") :
                                new ClientRequest()
                                {
                                    Id   = id,
                                    Name = typeof(T).Name,
                                    Time = DateTime.UtcNow
                                };

            _context.Add(request);

            await _context.SaveChangesAsync();
        }
コード例 #9
0
        public string Save(CompetitionModel model)
        {
            Mapper.Initialize(cfg => {
                cfg.CreateMap <CompetitionModel, Competition>();
            });

            try
            {
                Competition item = Mapper.Map <Competition>(model);
                item.EditedOn = DateTime.Now;
                context.Add(item);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                return(ex.Message.ToString());
            }

            return(CommonResource.ResourceManager.GetString("SuccessMessage"));
        }
コード例 #10
0
        public IActionResult UpdateProducts([FromBody] SetProductsRequest request)
        {
            var order = context.Orders
                        .Where(o => o.Id == request.OrderID)
                        .Include(o => o.OrderDetails)
                        .FirstOrDefault();

            if (order != null)
            {
                try
                {
                    // First remove all OrderDetails
                    foreach (var orderDetail in order.OrderDetails)
                    {
                        context.Remove(orderDetail);
                    }
                    // Then add new ones
                    foreach (var product in request.Products)
                    {
                        var orderDetail = new OrderDetail()
                        {
                            OrderId = request.OrderID, ProductId = product.Id
                        };
                        context.Add(orderDetail);
                    }

                    // Persist changes
                    context.SaveChanges();

                    return(Ok());
                }
                catch (Exception ex)
                {
                    return(BadRequest(new { error = new { message = ex.Message } }));
                }
            }

            return(NotFound());
        }
コード例 #11
0
ファイル: Repository.cs プロジェクト: cemutku/DesignPatterns
 public virtual T Add(T entity)
 {
     return(_context.Add(entity).Entity);
 }