예제 #1
0
        public RepositoryActionResult<Conference> InsertConference(Conference e)
        {
            try
                {
                _ctx.Conferences.Add(e);
                var result = _ctx.SaveChanges();
                if (result > 0)
                {
                    return new RepositoryActionResult<Conference>(e, RepositoryActionStatus.Created);
                }
                else
                {
                    return new RepositoryActionResult<Conference>(e, RepositoryActionStatus.NothingModified, null);
                }

                }
              catch (Exception ex)
              {
              return new RepositoryActionResult<Conference>(null, RepositoryActionStatus.Error, ex);
              }
        }
예제 #2
0
        public RepositoryActionResult<Conference> UpdateConference(Conference e)
        {
            try
                {

                    // you can only update when an expense already exists for this id

                    var existingExpense = _ctx.Conferences.FirstOrDefault(exp => exp.Id == e.Id);

                    if (existingExpense == null)
                    {
                        return new RepositoryActionResult<Conference>(e, 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(existingExpense).State = EntityState.Detached;

                    // attach & save
                    _ctx.Conferences.Attach(e);

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

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