コード例 #1
0
 public DTO.Kid CreateKid(Kid kid)
 {
     return new DTO.Kid
     {
         Id = kid.Id,
         UserUnitId = kid.UserUnitId,
         FirstName = kid.FirstName,
         LastName = kid.LastName
     };
 }
コード例 #2
0
 public RepositoryActionResult<Kid> AddKid(Kid kid)
 {
     try
     {
         _ctx.Kids.Add(kid);
         var result =_ctx.SaveChanges();
         if (result > 0)
         {
             return new RepositoryActionResult<Kid>(kid, RepositoryActionStatus.Created);
         }
         else
         {
             return new RepositoryActionResult<Kid>(kid, RepositoryActionStatus.NothingModified);
         }
     }
     catch (Exception ex)
     {
         return new RepositoryActionResult<Kid>(kid, RepositoryActionStatus.Error, ex);
     }
 }
コード例 #3
0
        public RepositoryActionResult<Kid> UpdateKid(Kid kid)
        {
            try
            {
                // you can only update when an expense already exists for this id

                var existingKid = _ctx.Kids.FirstOrDefault(k => k.Id == kid.Id);

                if (existingKid == null)
                {
                    return new RepositoryActionResult<Kid>(kid, RepositoryActionStatus.NotFound);
                }

                // change the original entity status to detached; otherwise, we get an error on attach
                // as the entity is already in the dbSet

                // set original entity state to detached
                _ctx.Entry(existingKid).State = EntityState.Detached;

                // attach & save
                _ctx.Kids.Attach(kid);

                // set the updated entity state to modified, so it gets updated.
                _ctx.Entry(kid).State = EntityState.Modified;

                var result = _ctx.SaveChanges();
                if (result > 0)
                {
                    return new RepositoryActionResult<Kid>(kid, RepositoryActionStatus.Updated);
                }
                else
                {
                    return new RepositoryActionResult<Kid>(kid, RepositoryActionStatus.NothingModified, null);
                }
            }
            catch (Exception ex)
            {
                return new RepositoryActionResult<Kid>(null, RepositoryActionStatus.Error, ex);
            }
        }