![]() We can delete some rows and load all the related entities with the include method: This is simple and a commonly used pratice, but we have an alternative. ![]() Var post = await db.Posts.FirstAsync(p => p.Id = postEntity.Id) Īwait db.SaveChangesAsync().ConfigureAwait(false) Var tags = db.Tags.Where(c => c.PostId = postEntity.Id) Var categories = db.Categories.Where(c => c.PostId = postEntity.Id) Now if we want to delete the Post entity and the related we can do this: Suppose that we have a Post table, with the related entities Categories and Tags of course the last two have a foreign key to the Post table: (Line: 12) The 'NoContent()' status code is '204'.Entity framework has some features and peculiarities that are useful in a lot of situations and with they can help us to do some operations in very smart and efficient mode.Īnyway in some circumstances it could surprise us with some very strange side effects or behaviors that may we don’t know we can meet one of these in the deletion of an entity and the related entities.The 'SaveChangesAsync()' update the database context to delete the record. (Line: 10-11) Removing the record from the database context.If no valid record in the database then we return 'NotFound()' method status code is '404'. (Line: 6-9)Validating the record to be deleted.(Line: 5)Fetching the record that needs to be deleted from the database.(Line: 3-13) Asynchronous action method for deleting the record from the database.(Line: 2) The 'HttpDelete' attribute represents our action method getting invoked only for the HTTP DELETE requests.Return Created($"/get-student-by-id?id=' expression means any string defined inside of it will be the input parameter to the action method. Public async Task PostAsync(Students student)Īwait _myWorldDbContext.SaveChangesAsync() Public async Task GetStudentByIdAsync(int id) (Line: 7) The 'Ok()' method creates an OkObjectResult that produces a StatuCodes.Status200Ok(which means 200 success) response.Ĭontrollers/StudentsController.cs:.The 'ToListAsync' is an extension from the 'Microsoft.EntityFrameworkCor'. (Line: 6) Fetching all records from the 'Students' entity as an async call.(Line: 4-8) Defined an asynchronous action method to return the collection of data.(Line: 3) The 'HttpGet' attribute represents that our action method gets invoked for the HTTP GET requests.But if we have multiple action methods of the HTTP verb types like 'GET', 'POST', 'PUT', and 'Delete' then we have to define the routes per action method explicitly. By convention, API will execute the Action methods based on the HTTP verb headers available for every request. If our controller contains a single HTTP GET action method then it is optional to add the 'Route' attribute. Here we can observe there is no 'Route' attribute on the action method. (Line: 10-14) Injected our 'DatabaseContext'(MyWorldDbContext) into our controller constructor.Ĭontrollers/StudentsController.cs: using Microsoft.EntityFrameworkCore.(Line: 8) To make our 'StudentsController' class an API controller it has to inherit the ''.We can define the custom route that can be different from the controller name as well. The default expression '' means the URL will contain the controller name(eg: 'Students' will be used in the route). (Line: 7) Rout attribute defines the URL for API.So it is essential to configure for any controller. It provides the API features to the controller, something like API versioning won't work if we don't use the attribute. (Line: 6) Decorated with the 'ApiController' attribute.Public StudentsController(MyWorldDbContext myWorldDbContext) Private readonly MyWorldDbContext _myWorldDbContext Public class StudentsController: ControllerBase Controllers/StudentsController.cs: using
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |