コード例 #1
0
        public async Task <IHttpActionResult> CreateUser(User user)
        {
            Profile profile = new Profile();

            if (ModelState.IsValid)
            {
                user.Password = HashService.HashPass(user.Password);
                using (var context = new ProfileDbContext())
                {
                    using (var dbContextTransaction = context.Database.BeginTransaction())
                    {
                        try
                        {
                            context.Users.Add(user);
                            await context.SaveChangesAsync();

                            profile.UserId = user.Id;
                            context.Profiles.Add(profile);
                            await context.SaveChangesAsync();

                            dbContextTransaction.Commit();

                            return(Ok());
                        }catch (Exception)
                        {
                            dbContextTransaction.Rollback();
                            var responseMessage = new HttpResponseMessage(HttpStatusCode.Conflict);
                            throw new HttpResponseException(responseMessage);
                        }
                    }
                }
            }

            throw new HttpResponseException(HttpStatusCode.BadRequest);
        }