Hello All,
In this article we will develop Simple CRUD Operation Application Using ASP .NET Core API , For Database Side we are using MS SQL Server.
After this tutorial you will be able to develop the web API quickly.
Then why to wait, Lets start.
Introduction:
In this tutorial we well develop API to perform CRUD Operations. Through API we will be able to create, update, delete and select the Items.
Software Requirement:
Below mentioned software's are required to implement/develop the application.
1. Microsoft Visual Studio Latest Community Version(Click Here)
2. MS SQL Server Latest Express Edition(Click Here)
Designing the Web API:
For database connectivity and operations we are using the Entity Framework Core.
Note that we are using Code First Approach, Means Based on Classes in the application we will design the database.
1. Open Visual Studio
2. Create a New Project
3. Choose project "ASP.NET Core Web API"
4. Enter Project Name, Select Location and Clock Next
5. Select Framework .NET Core 6.0 and Click Create
6. Delete the un-used classes and objects(deleted the highlighted class files)
7. Lets add dependencies using nuget package manager which is required for database connectivity and operations.
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Tools
namespace My.Simple.Web.API.Models { public class Learning { public int LearningId { get; set; } public string Title { get; set; } public string Description { get; set; } public DateTime DateStarted { get; set; } public DateTime DateCompleted { get; set; } } }
using Microsoft.EntityFrameworkCore;
namespace My.Simple.Web.API.Models
{
public class AppDbContext:DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options):base(options)
{
}
public DbSet<Learning> Learning { get; set; }
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DevConnection" : "Server=SERVERNAME;Initial Catalog=MySimpleWebAPI_DB;
User Id=USERID;Password=********"
}
}
AppDbContext begins when the instance is created and ends when the instance is disposed.UseSqlServer Configures the context to connect to a Microsoft SQL Server database.
using Microsoft.EntityFrameworkCore;
using My.Simple.Web.API.Models;
var builder = WebApplication.CreateBuilder(args);
ConfigurationManager Configuration = builder.Configuration;
// Add services to the container.
builder.Services.AddDbContext<AppDbContext>(opt =>
opt.UseSqlServer(Configuration.GetConnectionString("DevConnection")));
add-migration "User Defined Comments"
update-database
[HttpGet("learning-list")]
public IActionResult Get()
{
return Ok(_context.Learning.ToList());
}
[HttpPost("learning-create")]
public IActionResult Create(Learning learning)
{
_context.Learning.Add(learning);
_context.SaveChanges();
return Ok("success");
}
[HttpPost("learning-update")]
public IActionResult Update(Learning learning, int learningId)
{
var learningToUpdate = _context.Learning.Where(o => o.LearningId ==
learningId).FirstOrDefault();
if (learningToUpdate == null)
return BadRequest();
learningToUpdate.Title = learning.Title;
learningToUpdate.Description = learning.Description;
learningToUpdate.DateStarted = learning.DateStarted;
learningToUpdate.DateCompleted = learning.DateCompleted;
_context.Learning.Update(learningToUpdate);
_context.SaveChanges();
return Ok("success");
}
[HttpDelete("learning-delete")]
public IActionResult Delete(int learningId)
{
var learningToDelete = _context.Learning.Where(o => o.LearningId ==
learningId).FirstOrDefault();
if (learningToDelete == null)
return BadRequest();
_context.Learning.Remove(learningToDelete);
_context.SaveChanges();
return Ok("success");
}