ASP.Net Core - Web API reverse Engineered.pdf - Asp.Net...

This preview shows page 1 - 13 out of 33 pages.

Asp.Net Core Web API By: 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 API CRUD operations (scaffold controller) Database (reverse engineered Models) Testing API using Fiddler Using Swagger
Database Setup First of all lets create a simple table in a UserDB in the database. Inside the UserDB lets create a table named UserTable.
Project Setup Lets create an ASP.Net Core Web Application project. I named it UserSystemAPI
Choose API form the template for your 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.SqlServer Microsoft.EntityFrameworkCore.Tools Microsoft.VisualStudio.Web.CodeGeneration.Design
Creating the connection with Database For creating the connection with the database go to Tools Connect to Database
Reverse Engineer Our Models Its time to reverse Engineer our Models from the existing database (Database First Approach). On the Package Manager Console run the following command Scaffold-DbContext “Server=your server name;Database=UserDB;Trusted_Connection =True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Once the command will run successfully you will see in your Solution Explorer Models Folder gets created UserDBContext.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 OnConfigure method in the last slide was to introduce the Dependency Injection in our project.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture