How to Update a data into database in ASP.NET Core Web API

  

ASP.NET Core API : How to Update or edit a record, Update or edit a record in sql,Update or edit  a  record, crud operation in ASP.Net Core Web API, CRUD operation in AsP.Net Core, Retrieve,Insert,Delete, Update in ASP.NET API, ASP.NET Core API , web api, asp.net web api, code-first entity framework,swagger, Insert or Create a record using code-first entity framework, how to Update or edit a record using asp.net core web api, write web api for Updating or editing records,  write web api for Updating or editing records using asp.net core web api

 In this article, I will tell you how to update a record in the SQL Database table. For updating a record in the SQL table we will use the code-first entity framework. In my previous article, I already described how to do settings for writing ASP.NET Core Web API.  Follow the steps:


Step-1:

I already discuss the table structure in my previous article. Please follow that article for creating a table structure using the Code-first entity framework. Also, I discuss the migration of models and updating your database in the SQL server. 

Step-2:

In your ASP.NET Core application, Create a Controller Page "ProfileController" inside the Controller folder. Then write API for updating a single record in the "Profile" table.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProfileWebApi.Model;
using Microsoft.EntityFrameworkCore;

namespace ProfileWebApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ProfileController : ControllerBase
{
public ApplicationDbContext DbContext;
public ProfileController(ApplicationDbContext applicationDbContext)
{
DbContext = applicationDbContext;
}
[HttpPut]
public ActionResult<Profile> UpdateProfile([FromForm]Profile profile)
{
DbContext.SaveChanges();
return profile;
}
}
}


Step-3:

Then Build your project. After build successfully completed then click on start debugging. then edit your URL. Run your localhost URL with "/swagger/index.html ". For example:



Step-4:

Then this page will open.

ASP.NET Core API : How to Update or edit a record, Update or edit a record in sql,Update or edit  a  record, crud operation in ASP.Net Core Web API, CRUD operation in AsP.Net Core, Retrieve,Insert,Delete, Update in ASP.NET API, ASP.NET Core API , web api, asp.net web api, code-first entity framework,swagger, Insert or Create a record using code-first entity framework, how to Update or edit a record using asp.net core web api, write web api for Updating or editing records,  write web api for Updating or editing records using asp.net core web api






Step-5:

Click on PUT​/api​/Profile​/UpdateProfile Then this page will open:

ASP.NET Core API : How to Update or edit a record, Update or edit a record in sql,Update or edit  a  record, crud operation in ASP.Net Core Web API, CRUD operation in AsP.Net Core, Retrieve,Insert,Delete, Update in ASP.NET API, ASP.NET Core API , web api, asp.net web api, code-first entity framework,swagger, Insert or Create a record using code-first entity framework, how to Update or edit a record using asp.net core web api, write web api for Updating or editing records,  write web api for Updating or editing records using asp.net core web api






Click on the "Try It Out" button. After filling up these details "Execute" button will show then click on that button. 

ASP.NET Core API : How to Update or edit a record, Update or edit a record in sql,Update or edit  a  record, crud operation in ASP.Net Core Web API, CRUD operation in AsP.Net Core, Retrieve,Insert,Delete, Update in ASP.NET API, ASP.NET Core API , web api, asp.net web api, code-first entity framework,swagger, Insert or Create a record using code-first entity framework, how to Update or edit a record using asp.net core web api, write web api for Updating or editing records,  write web api for Updating or editing records using asp.net core web api



Step-6: 

The result of your API will show. i.e. the data will update in the SQL server and the output will show like below.

 
ASP.NET Core API : How to Update or edit a record, Update or edit a record in sql,Update or edit  a  record, crud operation in ASP.Net Core Web API, CRUD operation in AsP.Net Core, Retrieve,Insert,Delete, Update in ASP.NET API, ASP.NET Core API , web api, asp.net web api, code-first entity framework,swagger, Insert or Create a record using code-first entity framework, how to Update or edit a record using asp.net core web api, write web api for Updating or editing records,  write web api for Updating or editing records using asp.net core web api




That's it. I hope you have found this article helpful. Do let me know via comments and likes.

CRUD Operation Reference Link:

1. Create
4. Update
5. Delete

Post a Comment

3 Comments

  1. Hi Parismita,
    I have been following your posts to learn writing ASP.NET Core API, thanks for all the good work. It happens when I followed this post creating the PUT update, I could get the success screen as same as your picture above, but the database has just not been updated, no error or such, could hint me where to look at for troubleshooting please?

    Cheers
    Jacky

    ReplyDelete
    Replies
    1. Thanks for your complement...
      For this issue check connectionstring in appsettings ..
      Reference link:
      https://c-sharpcodedestination.blogspot.com/2020/07/settings-for-aspnet-core-web-api-using.html

      Delete
    2. DBcontext.Update(.....) is missing

      Delete