Reputation: 51
I've configured the ASP.NET Core Web API using controller to use Sqlite as the database, and after the request of adding new item, the item is not being saved to the database.
The POST
request endpoint is as follows:
[HttpPost("{cartId}/items", Name = "Adding item to Cart")]
public async Task<IActionResult> PostItemToCart(Guid cartId, [FromBody] CartAddItemDTO input)
{
var cart = await _context.Carts.Include(c => c.Items).FirstOrDefaultAsync(c => c.CartId == cartId);
if (cart == null)
{
return BadRequest("The cart Id does not exist");
}
var product = await _context.Products
.FirstOrDefaultAsync(p => p.Id == input.ProductId);
if (product == null)
{
return BadRequest("The product id does not exist");
}
cart.Items ??= new List<Product>();
cart.Items.Add(product);
await _context.SaveChangesAsync();
return Created();
}
The ApplicationContextDB
and models used in the code are under the Models
folder.
I went through the C# debugger line by line and it looks like the Items in the cart is being added, just before the SaveChangesAsync()
. However it doesn't look like the schema of cart is not being updated after the execution of the SaveChangesAsync()
. I added the database debugger and the last SQL command being executed just before the save is the following:
Executed DbCommand (1ms) [Parameters=[@__input_ProductId_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT "p"."Id", "p"."CartId", "p"."Category", "p"."CurrentStock", "p"."Manufacturer", "p"."Name", "p"."Price"
FROM "Products" AS "p"
WHERE "p"."Id" = @__input_ProductId_0
LIMIT 1
What I was expecting is some sort INSERT INTO
command but that doesn't seem to be happening. The issue seem to arise somewhere between cart.Items.Add(product);
and await _context.SaveChagnesAsync();
, and I can't seem to narrow down what the issue could be.
Another issue I can think of is the entity/configuration of the database and the way that I mapped one-to-many relationship between Cart
(one) and Product
(many). The list of products in Cart
entity is called Items
. In the ApplicationDbContext.cs
, I have the following code on the method OnModelCreating
:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Seeding some data for Product table...
// Entity Mapping Starts here
modelBuilder.Entity<Product>()
.HasOne(p => p.Cart)
.WithMany(c => c.Items)
.HasForeignKey(p => p.CartId);
base.OnModelCreating(modelBuilder);
}
Upvotes: 0
Views: 63