Seed Entity Framework Code First With JSON Using C# Dynamic

If you just want the short version, go to the this GitHub Repository and you’ll find a very simple project that I’ll explain in more detail here.

https://github.com/pkellner/EntityFrameworkSeedFromJSON

Below are the steps necessary to achieve converting JSON into Entity Framework Seeded data.

Copy the JSON file into your Visual Studio project and make the build action (file property in VS) Embedded Resource.

image

Next, find out the name of the embedded resource. If you just don’t know it or don’t want to spend time disassembling the project you can use the GetManifestResourceNames() method as shown below.  I find that is quicker and I don’t typically have those tools loaded when I need or want then.

image

Once we have the manifest name, we can get the json file as a string and then feed that through NewtonSoft.JSON.  Then, using dynamic data we simply do the expected “Seed” with Entity Framework.  Below is the code (as well as in the GitHub Rep).


HTH’s

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP for the past 7 years. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Follow

Get every new post delivered to your Inbox

Join other followers: