
If you are new to.NET Core, read these articles to kick start your .NET Core knowledge.
- Creating A New .NET Core Web Application Using Visual Studio 2015
- Creating A New .NET Core Web Application With Command Line Interface (CLI)
- Setting And Reading Values From "appsettings.json" In .NET Core
- Binding Dropdown List With Database In ASP.NET Core MVC
- Creating Simple Cascading DropDownList In ASP.NET Core MVC With New Tag Helpers
Topic
- Database part.
- Creating an Application.
- Installing Package for Entity Framework Core From NuGet.
- Adding Connection string and setting up DbContext.
- Adding Category Model in Models folder.
- Adding DbSet for Category Model in DatabaseContext class.
- Adding Controller [DemoController].
- Getting the data from the database, using Entity Framework Core for binding RadiobuttonList.
- Adding View [index.cshtml].,
- Binding Radiobuttonlist using new Tag Helper.
- Adding Index Action method for handling the post request and getting the selected values.
- Saving and running the Application.
Step 1
Database part
Create a sample database with the name “AllSampleCode” to show the demo.

Inside this database, I have added Category table, where you can see the structure of the tables given below.

Step 2
Create a sample database with the name “AllSampleCode” to show the demo.

Inside this database, I have added Category table, where you can see the structure of the tables given below.

Step 2
Creating an Application
Now, let’s create .NET Core web application; open Visual Studio IDE. From the start page, click on New Project link.

After clicking on New Project link, it will open a new dialog with the name “New Project”. Inside it, from the left pane, choose the templates. Inside it, choose Visual C#. Inside it, choose.NET Core template and in middle of your pane, you will see.NET Core project templates. Subsequently, from the templates, choose “ASP.NET Core Web Application (.NET Core)” project templates.

After choosing the project template, now we are going to name the project as “MVCCore2” and finally click OK button to create a project, but it will pop up another dialog with the name “New ASP.NET Core Web Application (.NET Core)”.

Inside this dialog, we are going to choose “Web application” project template to create “Web Application” and click OK button to create a project.
Below is complete project view, which is newly created.

After creating an Application, we are going to add the reference required for Entity Framework Core.
Step 3
Now, let’s create .NET Core web application; open Visual Studio IDE. From the start page, click on New Project link.

After clicking on New Project link, it will open a new dialog with the name “New Project”. Inside it, from the left pane, choose the templates. Inside it, choose Visual C#. Inside it, choose.NET Core template and in middle of your pane, you will see.NET Core project templates. Subsequently, from the templates, choose “ASP.NET Core Web Application (.NET Core)” project templates.

After choosing the project template, now we are going to name the project as “MVCCore2” and finally click OK button to create a project, but it will pop up another dialog with the name “New ASP.NET Core Web Application (.NET Core)”.

Inside this dialog, we are going to choose “Web application” project template to create “Web Application” and click OK button to create a project.
Below is complete project view, which is newly created.

After creating an Application, we are going to add the reference required for Entity Framework Core.
Step 3
Installing Package for Entity framework core From NuGet
To install the package, just right click on the project (MVCCore2) and then select Manage NuGet package. The dialog of NuGet Package Manager will pop up. Inside it, the browse tab is there, where you need to search box type “Microsoft.EntityFrameworkCore.SqlServer” and just click on Install button to install.
To install the package, just right click on the project (MVCCore2) and then select Manage NuGet package. The dialog of NuGet Package Manager will pop up. Inside it, the browse tab is there, where you need to search box type “Microsoft.EntityFrameworkCore.SqlServer” and just click on Install button to install.
- Microsoft.EntityFrameworkCore.SqlServer

Step 4
Adding Connection string and Setting up DbContext
After adding the reference, next step is to add a connection string in appsetting.json file.

After adding the connection string, the next step is to add a class, which will inherit DbContext class, but before doing this, let's start with creating a folder for Models and inside it, we are going to add this class.
For adding a folder, just right click on the project (MVCCore2), followed by choosing Add from menu that pops up and inside it, choose New Folder.
Add - New Folder.

Now, let’s add a class with the name DatabaseContext in Model folder.
To add model, just right click on Models folder, followed by selecting Add. Inside it, select Class and Add New Item dialog will pop up with the default class selected, followed by going to name class as DatabaseContext and click on Add button.

After adding a DatabaseContext class, we are going to inherit DbContext class.
After inheriting with DbContext, we are having created a constructor, which takes DbContextOptions as an input parameter and also inherits the base class constructor (: base(options)) [DbContext].

Now, we are going to add new Service in Startup.cs class for injecting dependency.
Now, whenever you are going to use DatabaseContext class, DbContext instance will be injected.

After completing with add service, we are going to add Model
Step 5















Join the conversation! Your thoughts help the community grow.