Ejemplo n.º 1
0
        public async Task <T> Create(T body)
        {
            _db.Add <T>(body);
            await _db.SaveChangesAsync();

            return(body);
        }
Ejemplo n.º 2
0
        public ResultModel Create(ProjectModel entity)
        {
            var     result = new ResultModel();
            dynamic output;

            var project_partner_list = new List <project_partnerModel>();

            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    _context.Add(entity);
                    var except_project_save = _unitOfWork.Save();
                    if (except_project_save != null)
                    {
                        throw except_project_save;
                    }
                    output = new
                    {
                        project_id = entity.id
                    };

                    if (!string.IsNullOrEmpty(entity.partner_id))
                    {
                        //新增合作廠商id 到project_partner table (註)
                        string[] partner_id_arr = entity.partner_id.Split(",");
                        foreach (var i in partner_id_arr)
                        {
                            var data = new project_partnerModel()
                            {
                                created    = DateTime.Now,
                                project_id = entity.id,
                                partner_id = Convert.ToInt32(i)
                            };

                            _repo_project_partner.Create(data);
                            project_partner_list.Add(data);
                        }

                        var except_partner_save = _unitOfWork.Save();
                        if (except_partner_save != null)
                        {
                            throw except_partner_save;
                        }

                        output = new
                        {
                            project_id         = entity.id,
                            project_partner_id = project_partner_list.Select(a => a.id)
                        };
                    }
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    result = ErrorCode.CUSTOM_ERROR(e.Message, "DB ERROR", 500, "cht");
                    return(result);
                }
            }

            result = ErrorCode.SUCCESS(output, "", "cht");
            return(result);
        }