Using map instead of a for loop in ECMAScript 5 JavaScript




Just Sayin…

Seeding a Many to Many With Entity Framework and C# Dynamic

In the previous post, we created a single table (speakers) with Entity Framework Code First from a JSON file with Visual Studio 2015.  Now we will add a sessions table that has a many to many relationship to the speakers table.  That is, speakers can have multiple sessions and sessions can have multiple speakers.  We do that but simply creating another entity (sessions) and add to it a List of speakers and then adding to the existing speakers table a list of sessions.  The new table (models) look as follows.

GitHub Repo:

Then,  we need to add sessions data (from the JSON file) but when we add the sessions, we need to add the speakers records associated with each session.  That is, we simply populate the List of sessions in the speaker model to make this happen.

One problem we ran into was that EF Code First really wants to control the Id of each record and since in our original data, the many to many relationship was maintained by original primary keys, we need to bring along the original speaker Id.  We do this by simply adding an extra column to the speaker model called PictureId and set that as an attribute (column) of the speaker table.  Then, we can use that to search the speakers table for the existing speakers.

The code below (and also in the GitHub repo) basically does this for us.  It again uses NewtonSoft.JSON as well as the dynamic type in c# to get the job done.

To test the results, in Program.cs we put some simple loops and Console Write’s to show.


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.

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.


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.


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).


GitHub Visual Studio 2015 Extension, Getting Started

I’ve been using Git as my source control of choice for years now.  I’ve recently discovered the GitHub Visual Studio extension that takes some of the hassles away from this process.  Specifically, creating a new Git Repository from a Visual Studio 2015 project and publishing it to GitHub is always a small dance.  Now, it’s trivial and with this blog post, if I ever forget how (or you do), the simple steps and screen shots are here.

Step 1 – Make sure you have the Visual Studio 2015 Git Extension installed.  To do that, simply go to Team/Manage Connections



and then verify the extension is installed (or install it)



Step 2 – Create a Visual Studio 2015 Project

Just like any other project you would create with Visual Studio 2015, you simply say File/Project/New and choose a project type (I’m going to choose C# console project as shown below (make sure you choose create new Git Repository as shown by my red arrow).



Step 3 – Check into GitHub

You’ll then see the console project as follows:


Now, from here, press the “HOME” path (little house icon on above screen shot near top left) and you will get the following screen:


Then, click on “Changes” and create your first commit (after typing in your commit message as I have done, then pressing the commit button.


You will get the message that the commit has been made as shown below.  This however does not actually push the changes to GitHub (remember, Git maintains the repo locally and you need to sync it to get it to GitHub).


To get the changes synchronized to GitHub you need to click on the blue “Sync” button in the commit message above and then you will get a dialog to push your changes to a new GitHub Repo as follows:


Step 4 – Create a GitHub Repo and Synchronize VS Project


You will be rewarded with the success message and if you look on GitHub you will see the repo with your project files.




Step 5 – Make a Source File Change and Commit and Push Changes

The last thing to make this article somewhat complete is to make a simple change to a source file (Program.cs in my case) and commit that change to Git and push to GitHub.  You can do that in one step on the Team Explorer – Changes window.  Simply activate the chevron next to the commit button and select “Commit and Push” as follows:


And of course the success message is always nice to see:


Now, if we look at GitHub Repository, we can see the change made it!



That’s it!  very easy.  You can still use other tools for managing your git files outside of Visual Studio.  Git is keeping track of the data (not VS) so all updates will be reflected every place.



Other Resources:

Helpful Video by Phil Haack
Visual Studio GitHub Extension

Plugin by Social Author Bio


Get every new post delivered to your Inbox

Join other followers: