
We are going to learn little things on how to setup Entity Framework Core in .NET Core Web Application. Developers coming from a background of ASP.NET MVC will find it easy to work with ASP.NET Core MVC.
If you are new to .NET Core read these articles to kickstart 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)
Topic
- Database part.
- Creating an Application.
- Installing package for an Entity Framework Core From NuGet.
- Adding the Connection string and Setting up DbContext.
- Adding Model CountryMaster in Models Folder.
- Adding DbSet for CountryMaster Model in DatabaseContext class.
- Adding Controller.
- Getting Data from the database, using Entity Framework Core.
- Adding View.
- Binding Dropdownlist, using new Tag helper.
- Adding Index Action method to handle POST request and getting the selected values.
- Saving and running an Application.
Step 1
Create a sample database with the name “AllSampleCode” to show demo

Inside this database, I have added a simple table, CountryMaster, and you can see the structure, as shown below.

Step 2
Now, let’s create a .NET Core Web Application. Open Visual Studio IDE from the start page and click New Project link.

Afterwards, click New Project link and it will open a new dialog with the name “New Project” inside it. From the left pane, choose templates and inside it, choose Visual C#. Afterwards, choose .NET Core template. In the middle of your pane, you will see .NET Core project templates. In templates, choose “ASP.NET Core Web Application (.NET Core)” project templates.

After choosing project template next we are going to name the project as “MVCCore1” 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 the newly-created complete project view

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

Step 4
After adding the reference, there is a need to add a connection string in appsetting.json file.

After adding the connection string, next step is to add a class, which will inherit DbContext class, but before doing this, let's start 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 (MVCCore1), then choose Add from Menu that pops up and inside it and choose New Folder.
Add - New Folder.

Now, let’s add a class with the name DatabaseContext in model folder.
For adding model, just right click on Models folder, then select add. Inside it, select Class and add New Item; a dialog will pop up with default class selected, followed by naming class as DatabaseContext and clicking Add button.

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

Now, we are going to add new Service in Startup.cs class to inject 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
For adding model, just right click on Models folder and select Add. Inside it, select Class and add New Item. The dialog will pop up with default class, which is selected, then we are going to name class as CountryMaster and click Add button.

Step 6
After adding CountryMaster Model, now let's add DbSet for CountryMaster Model in DatabaseContext class, as shown below.

Step 7















Ashok KumarPosted Jul 4, 2021, 10:12 AM
Thank you very much for this wonderful post, Saineshwar.
Katherine HambleyPosted Mar 25, 2021, 1:10 PM
Excellent job! I modified mine to create the SelectList in the controller instead of the view, but it works great! Thank you so much!
Shahrukh AlamPosted Jan 9, 2020, 4:11 AM
Great article
Rahul PatilPosted Oct 16, 2019, 2:15 AM
When i inserted data in database then give an error:Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList..ctor(IEnumerable items, string dataValueField, string dataTextField, IEnumerable selectedValues, string dataGroupField) asp-items="@(new SelectList(@ViewBag.ListofCountry,"countryId","countryname"))"></select>
Tony TrappPosted Sep 9, 2019, 12:36 PM
Hi Saineshwar, I have a problem when I click on the first dropdown then second dropdown is populated but only one item, what I am doing wrong, I have three with the first select dropdown.
Vikas VikasPosted Sep 8, 2019, 2:51 PM
Hello sir thanks for greate article can we update realtime data from database using signalr in same this cascading
mark liddlePosted Oct 24, 2018, 5:50 PM
Great article....thanks. Unfortunately I seem to be missing something. …. In Step 8, there is a line QUOTE countrylist = (from product in _context.CountryMaster select product).ToList(); UNQUOTE. However, I haven't been able to find a class/object named 'product' in your example. Should this read 'Id' or 'Name' (referring to a field in the country table) ?
Adalat KhanPosted Sep 22, 2018, 6:01 PM
Outstanding article. Thanks for sharing
Jose Jr.Posted Jul 11, 2018, 1:10 AM
This guide is inserting ID in database, what will i change to make it insert the name?
Tochinicky OnyeamahPosted Jul 2, 2018, 10:20 AM
Please how can i implement this without mvc because i'm using asp.net core 2.0 without mvc
Alexis BenardPosted Jun 5, 2018, 7:13 AM
Hello ! It's functional for me but if i would like create an other Combobox, how can we do ? Because we can't use 2 models..
ajit rathPosted Mar 13, 2018, 2:08 AM
Sir, i have a problem in app.UseIdentity(); .now what can i do ?please help [email protected]
Debendra DashPosted Jan 19, 2017, 10:53 PM
Excellent article sir............
Manav PandyaPosted Dec 12, 2016, 8:46 AM
Nice article as always sir Saineshwar Bageri