Skip to content

Instantly share code, notes, and snippets.

@driscollwebdev
Created January 15, 2013 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save driscollwebdev/4539130 to your computer and use it in GitHub Desktop.
Save driscollwebdev/4539130 to your computer and use it in GitHub Desktop.
An example of parsing an object's data from its representation and saving it somewhere all in one method (not the best approach).
public void SaveFromJson(string jsonData, DbContext saveContext)
{
try
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
Person person = serializer.Deserialize<Person>(jsonData);
//This is just an example, but in prod code you'd need
//to make sure the entity doesn't already exist.
saveContext.Entry<Person>(person).State = System.Data.EntityState.Added;
saveContext.SaveChanges();
}
catch (ArgumentException argEx)
{
//TODO: Handle this exception.
}
catch (ArgumentNullException argNullEx)
{
//TODO: Handle this exception.
}
catch (InvalidOperationException badOpEx)
{
//TODO: Handle this exception.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment