It is easy to insert, update, and delete entities using EF Core.
At the bottom of the Main
method, after the foreach
statement, add the following code to insert a new product and relist all products:
var newProduct = new Product { CategoryID = 6, // Meat & Poultry ProductName = "Bob's Burger", UnitPrice = 500M }; // mark product as added in change tracking db.Products.Add(newProduct); // save tracked changes to database db.SaveChanges(); foreach (var item in db.Products) { WriteLine($"{item.ProductID}: {item.ProductName} costs {item.UnitPrice:$#,##0.00}"); }
Rerun the application and enter 50
. You will see that the product has been inserted:
78: Bob's Burger costs $500.00