The Product Management module provides full Create, Read, Update, and Delete (CRUD) functionality for managing supermarket inventory. Products are linked to categories and include pricing and stock information.
The Product model (Models/Product.cs:5-15) defines the structure for product data:
public class Product{ public int Id { get; set; } public string Name { get; set; } [Column(TypeName = "decimal(6,2)")] public decimal Price { get; set; } public int Stock { get; set; } public int CategoryId { get; set; } public Category? Category { get; set; }}
Products are linked to categories through a foreign key relationship:
public int CategoryId { get; set; }public Category? Category { get; set; }
This allows products to be organized and filtered by category. The Category navigation property enables loading related category information when needed.
Always validate user input using ModelState before saving to the database. The Product model should include appropriate data annotations for validation.
Error Handling
The edit operation includes concurrency exception handling to prevent data conflicts. Consider adding try-catch blocks for other operations as well.
Decimal Precision
The decimal(6,2) type ensures accurate price calculations without floating-point errors. Always use decimal types for monetary values.
Navigation Properties
The nullable Category? navigation property allows lazy loading of related category data when needed, reducing unnecessary database queries.