Performing seed data to database from JSON using reflection

We can work with two approaches in Asp.Net MVC i.e., DB first approach and Code first approach. Apart from that, we can also work with different ORM models such as Entity framework, NHibernate, etc.

In the code first approach, database create with the migration is simple and easy task to add, update, and delete the data. It helps seed database simply, just add the properties and call the Savechanges method.

We can initialize the code first generated database with the values from JSON using data access layer and reflection. By the way, JSON is considered as the best data format as it gives clean and clear data result. We can apply other formats to the same approach but JSON is used for the ease and the availability of the large and optimal JSON.NET library from Newtonsoft.

What is Reflection in core?

Reflection allows us to get information about the assemblies and type defined within them such as classes, interface, structure, and enumerations.

Using reflection, we can dynamically instantiates the objects, access the fields, methods, properties. Reflection can lower down the performance. If we accessed the fewer objects with reflection then we will not notice the execution speed. Let’s take a simple example to understand it.

The execution time for the first one method is faster than the reflection get method. So the best way is to not use the reflection with our project as it will significantly reduce the performance.

Seeding with JSON data

Step 1: Add JSON file

Now we will add the JSON file with the name Customer.json as follows. Here we have the simple data with three to four property (Name, order, item).

Now we will use this code to seed the database. We are calling it from the start up file in configure method in asp.net core.

Step 2: Update Startup file

Now we will update the configure method in the startup file. In which we will read the file with System.IO.File.ReadAllText. After that we will add the json text in seed method as follows. In the read all text we have passed our JSON file.

Step 3: create the Seeding class

In this class, we will define the Seed method in it which is used for the deserialize the JSON file into the list of Customer object. After that we have used here the asp.net core service provider to get services from the startup file. In the deserialize object we have pass the json data and json serialize setting object into the list of customer.

After that call the addrange method into the customer and performed the savechanges method.

Let’s take another example by seeding the database using has data.

Seeding using has data

Step 1: Create model

Here we take the two model named Item and Customer as shown below. In which Customer id is the foreign key in the Item table.

Step 2: Update context file

Here we have to override the onmodelcreating method and fill the data in it. We can use it for adds multiple rows at a time. Migration can automatically compute the operation while updating the database of the model.

Step 3: Create the controller

Create the controller to get the data from the two models.

If you have large data file like JSON then you can go through as below. Just add a new method and call the json file from where it is placed and read that file using reader.

Conclusion:

In this blog, we have seen the programming concepts of how to seed the database using json and has data. Seeding using json is as simpler as you seen. First of all, we have to see the reflection but it consumes the execution time. So we have done with JSON file normally just write the file and call it in the startup file. Seeding using the JSON file, will turn our code lighter and simpler.