private void BTN_remove_Click(object sender, EventArgs e) { using (var db = new CrmAzureDbContext()) { int val; if (int.TryParse(TB_idRemove.Text, out val)) //Check if entry is a valid id format else ignore it { //Check if entry exists else just ignore the input var flag = (db.Customer.Where(p => p.Id == int.Parse(TB_idRemove.Text)).FirstOrDefault()); if (flag == null) { TB_idRemove.Text = String.Empty; return; } else { db.Entry(flag).State = Microsoft.EntityFrameworkCore.EntityState.Detached; } var customer = new Model.Customer() { Id = int.Parse(TB_idRemove.Text) }; db.Customer.Attach(customer); db.Customer.Remove(customer); db.SaveChanges(); } } TB_idRemove.Text = String.Empty; }
private void BTN_addOrdProd_Click(object sender, EventArgs e) { int prId = int.Parse(textBox2.Text); int ordId = int.Parse(textBox1.Text); using (var db = new CrmAzureDbContext()) { var orderproduct = new Model.OrderProduct() { ProductId = prId, OrderId = ordId }; db.OrderProduct.Add(orderproduct); db.SaveChanges(); } textBox1.Text = String.Empty; textBox2.Text = String.Empty; }
private void BTN_AddCust_Click(object sender, EventArgs e) { string name = TB_name.Text; string lname = TB_lname.Text; string ia = CB_IA.Checked.ToString(); using (var db = new CrmAzureDbContext()) { var customer = new Model.Customer() { FirstName = name, LastName = lname, IsActive = ia == "False" ? false : true }; db.Customer.Add(customer); db.SaveChanges(); } TB_name.Text = String.Empty; TB_lname.Text = String.Empty; CB_IA.Checked = false; }
private void BTN_addProduct_Click(object sender, EventArgs e) { string name = TB_name.Text; string code = TB_code.Text; decimal price = decimal.Parse(TB_price.Text); using (var db = new CrmAzureDbContext()) { var products = new Product() { Name = name, Code = code, Price = price }; db.Product.Add(products); db.SaveChanges(); } TB_name.Text = String.Empty; TB_code.Text = String.Empty; TB_price.Text = String.Empty; }
private void BTN_addOrder_Click_1(object sender, EventArgs e) { string descr = TB_descr.Text; var date = Convert.ToDateTime(dateTimePicker1.Text); string addr = TB_addr.Text; int custId = int.Parse(CB_custIds.Text); using (var db = new CrmAzureDbContext()) { var orders = new Order() { Description = descr, Created = date, Address = addr, CustomerId = custId }; db.Order.Add(orders); db.SaveChanges(); } TB_descr.Text = String.Empty; TB_addr.Text = String.Empty; CB_custIds.Text = String.Empty; }
static void Main(string[] args) { using (var db = new CrmAzureDbContext()) { // --------Added 30 customers --------------- //for (int i = 0; i < 30; i++) //{ // var customer = new Customer() // { // FirstName = $"Firstname {i} ", // LastName = $"Lastname {i} ", // IsActive = true, // Gross = 0 // }; // db.Add(customer); //--------------------------------------------- //--------Added 10 Orders(with 1 Product in each) to random Customers-------------------- //for (int i = 0; i < 10; i++) //{ // var randomCustomer = db.Customer // .OrderBy(r => Guid.NewGuid()) // .Skip(0) // .Take(1) // .Include(c => c.Orders) // .ThenInclude(o => o.Products) // .SingleOrDefault(); // var order = new Order() // { // Description = $"This is Description number {i}", // Created = DateTimeOffset.UtcNow, // Address = $"Address number {i} ", // Customer = randomCustomer, // CustomerId = randomCustomer.Id, // }; // Product product = new Product() // { // Name = "Huawei Y40", // Price = 200, // Code = Guid.NewGuid().ToString() //}; // List<OrderProduct> orderProducts = new List<OrderProduct>() // { // new OrderProduct() // { // Order = order, // Product = product, // OrderId = order.Id, // ProductId = product.Id // } // }; // order.Products = orderProducts; // randomCustomer.Gross += product.Price; // randomCustomer.Orders.Add(order); //} // -------------------------------------------------------------------------- //// Display Top 3 Customers //var TopGross = (from c in db.Customer // orderby c.Gross descending // select c).Take(3).ToList(); //TopGross.ForEach(a => { Console.WriteLine($"Customer Name: {a.FirstName} Surname: {a.LastName} Total Gross: {a.Gross}"); } //); //Console.ReadLine(); db.SaveChanges(); }; }