Create a sample Database table that holds the information of departments.
Let's create a .NET Core MVC application using VS 2019 with targeting framework 3.1 ( You can do it according to your IDE and version). If you are wonder how to create a .NET Core application then follow the link how to create .NET Core application in VS 2019
Step-1 : Installing Package for Entity framework core From NuGet
To install the package just right click on the project 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 like below;
Step-2 : Add the connection string in appsettings.json file like below;
Step-3 : Add a model inside Models folder and map into database table
After adding a DataContext 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, whenever you are going to use DataContext class, DbContext instance will be injected.
Step-6 : In HomeController.cs we are using constructor injection to get a dependency.
Now save the application and run and you can see the output like below;
</> Find the Source Code in Github.com/CoreProgramm/
Summary
Post a Comment