示例#1
0
        // -DtoQuery

        public async Task <StudentDto> CreateStudentAsync(StudentDto studentDto, string username)
        {
            OnCreate(studentDto, username);

            var entity = StudentDto.AsStudentFunc(studentDto);

            ToEntity(ref entity, studentDto);
            //entity.InsertUser = entity.LastActivityUser = username;
            //entity.InsertDateTime = entity.LastActivityDateTime = DateTime.UtcNow;
            entity.AddTracker(username);

            _context.Students.Add(entity);

            OnBeforeCreate(entity, username);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                // _context.Entry(entity).State = EntityState.Detached;
                throw new Exception("Add error", ex);
            }
            finally
            {
                // _context.Entry(entity).State = EntityState.Detached;
            }
            OnAfterCreate(entity, username);

            // studentDto = StudentDto.AsStudentDtoFunc(entity);
            studentDto = await GetStudentDtoAsync(entity.Id, StudentDto.IncludeNavigations());

            return(studentDto);
        }