Created
January 15, 2013 14:41
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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