100%(1)1 out of 1 people found this document helpful
This preview shows page 1 - 13 out of 33 pages.
Asp.Net CoreWeb APIBy: Mahboob Ali
Contents•The following lecture will cover the last of its series example on creating Web API with CRUD operations using reverse engineering.Web APICRUD operations (scaffold controller)Database (reverse engineered Models)Testing API using FiddlerUsing Swagger
Database Setup•First of all lets create a simple table in a UserDBin the database.•Inside theUserDBlets create a table named UserTable.
Project Setup•Lets create an ASP.Net Core Web Application project.•I named it UserSystemAPI
•Choose API formthe template foryour project.
Project setup•Once project is created, go to Tools→NuGet Package Manager →Manage NuGet Package for solutions•Then from the Browse find and install three packages for the project.Microsoft.EntityFrameworkCore.SqlServerMicrosoft.EntityFrameworkCore.ToolsMicrosoft.VisualStudio.Web.CodeGeneration.Design
Creating the connection with Database•For creating the connection with the database go toTools →Connect to Database
Reverse Engineer Our Models•Its time toreverse Engineer our Models from the existing database (Database First Approach).•On the Package Manager Console run the following commandScaffold-DbContext“Server=your server name;Database=UserDB;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDirModels
•Once the command will run successfully you will see in your Solution ExplorerModels Folder gets createdUserDBContext.cs file (responsible for our connection with database)UserTable.cs file (Model class from the database)
UserDBContext.cs file •Open your UserDBContext.cs file and delete or remove the method protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)•Your file will look like something like this
Startup.cs•The reason to delete or comment our OnConfiguremethod in the last slide was to introduce the Dependency Injection in our project.