This is a youtube tutorial to create an api using ASP.NET CORE 6, Entity Framework 6, and SQL Server DONE SUCCESSFULLY URL of the Tutorial => tutorial-link .
- Microsoft.AspNet.WebApi.Cors
 - Microsoft.EntityFrameworkCore
 - Microsoft.EntityFrameworkCore.Design
 - Microsoft.EntityFrameworkCore.SqlServer
 - Microsoft.EntityFrameworkCore.Tools
 - Microsoft.VisualStudio.Web.CodeGeneration.Design
 - Swashbuckle.AspNetCore
 
Create an ASP.NET Core Web API Projet, using Donet 6, EF 6.
install all the packages mentionned above.
Create C# classes, you can add them in models folders or in the root, in this project, we have 3 models on the root folder:
- Inspection
 - InspectionType
 - Status
 
Status.cs
using System.ComponentModel.DataAnnotations;
namespace InspectionAPI
{
    public class Status
    {
        public int Id { get; set; }
        [StringLength(20)]
        public string StatusOption { get; set; } = String.Empty;
    }
}InspectionType.cs
using System.ComponentModel.DataAnnotations;
namespace InspectionAPI
{
    public class InspectionType
    {
        public int Id { get; set; }
        [StringLength(20)]
        public string InspectName { get; set; } = string.Empty;
    }
}Inspection
using System.ComponentModel.DataAnnotations;
namespace InspectionAPI
{
    public class Inspection
    {
        public int Id { get; set; }
        [StringLength(20)]
        public string Status { get; set; } = String.Empty;
        [StringLength(20)]
        public string Comments { get; set; } = String.Empty;
        
        public int InspectionTypeId { get; set; }
        public InspectionType? InspectionType { get; set; }
    }
}NOTE:
Entity Framework will detect that InspectionTypeIdis a foreign key from the InspectionTypetable.
Create a folder on the root called data and create inside it a class called: DataContext
Add this code to DataContext file:
using Microsoft.EntityFrameworkCore;
namespace InspectionAPI.Data
{
    public class DataContext: DbContext
    {
        public DataContext(DbContextOptions<DataContext> options) : base(options) { }
        public DbSet<Inspection> Inspections { get; set; }
        public DbSet<InspectionType> InspectionTypes { get; set; }  
        public DbSet<Status> Statuses { get; set; }
    }
}NOTE: Add the Code blocks where its wrapped between comments add code from here to the comment to here
appsettings.json
{
// add this code from here
  "ConnectionStrings": {
    "DefaultConnection": "server=SAFWEN-ASUS-11\\SQLEXPRESS;database=inspectionapidb;trusted_connection=true"
  },
 // to here 
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}Now we need to update program.cs and configure Cors and Connection String:
NOTE: Add the Code blocks where its wrapped between comments add code from here to the comment to here
program.cs
using InspectionAPI.Data;
using Microsoft.EntityFrameworkCore;
// 1- add this from here
var myAllowSpecificOrigins = "_myAllowSpecificOrigins";
// 1- to here
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// 2- add this code from here
builder.Services.AddDbContext<DataContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});
// Enable Cors
builder.Services.AddCors(options =>
{
    options.AddPolicy(name: myAllowSpecificOrigins, builder =>
    {
        builder.WithOrigins("http://localhost:4200")
        .AllowAnyMethod()
        .AllowAnyHeader();
    });
});
// 2- to here
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
// 3- add this code from here
app.UseCors(myAllowSpecificOrigins);
// to here
app.UseAuthorization();
app.MapControllers();
app.Run();Now our DataContext, Models and the configuration to connect to database is done, we can now create the first migrations:
open package manager console and run:
IMPORTANT dotnet-ef is no longer a part of .NET SDK so you have to install it globally:
dotnet tool install --global dotnet-efNow let's add migrations:
dotnet ef migrations add InitialCreateThis command will create a folder on the root called migrations but it will not create or make any changes to the database, so to do it, run this command:
dotnet ef database updateNow you can check and see our database and tables are created.
Now its time to add the controllers, right click on controllers folders, select add then select controller, then select API on the left and select API Controllers with actions, using Entity Framework. Inside the dialog:
- For 
Model class: select Inspection(InspectionAPI) - For 
Data context class: select DataContext(InspectionAPI.data) - For 
Controller name: keep the default name generated InspectionsController 
IMPORTANT: DO THE SAME FOR ALL THE MODELS: STATUS, INSPECTIONS, AND INSPECTIONS
Thanks.