# Using EF 6.1 for updating objects and their children

I have an application that allows a user to create a poll that includes questions and answer choices as part of the poll. The hierarchy is a poll has one or more questions, a question has one or more answers, and each answer has a vote.

The following code is my implementation that allows the updating of an existing poll. The update allows for properties of the poll to be updated, including the addition or removal of children.

The implementation works, but it feels like there is a better way to achieve the functionality. As you will see from my code, I'm creating a context object each time I want to add or update any of the objects.

 public string Update(Poll poll)
{
try
{
if (poll == null)
return null;

using (var context = new PollContext())
{
context.Entry(poll).State = poll.PollId == Guid.Empty ? EntityState.Added : EntityState.Modified;
context.SaveChanges();
}

//update the questions
using (var context = new PollContext())
{
foreach (var question in poll.Questions)
{
context.Entry(question).State =
question.QuestionId == Guid.Empty ? EntityState.Added : EntityState.Modified;
}

context.SaveChanges();
}

using (var context = new PollContext())
{
{

using (var context2 = new PollContext())
{
{
{
}
}
}

{

}
}

context.SaveChanges();
}

return "Poll updated";
}
catch (NullReferenceException ex)
{
throw new NullReferenceException("A null exception occurred in updating poll.\r\n" + ex.InnerException);
}
catch (Exception ex)
{
throw new Exception("An exception occurred in the updating poll.\r\n" + ex.InnerException);
}

}


1. You don't have to use a using statement. You can use an IoC container such as Autofac and let it dispose the PollContext for you.

2. I think you probably should update the children collection in memory first and call context.SaveChange() only once at the end, so the whole update operations are wrapped with in a unit of a work.

3. I don't think try/catch is needed here as you only re-throw the exception, which could be handled on a higher level.

4. There's lots of repeating of:

context.Entry(poll).State = poll.PollId == Guid.Empty ? EntityState.Added : EntityState.Modified;