protected void Button5_Click(object sender, EventArgs e) { Model2Container db = new Model2Container(); //方式1 //var customer = (from c in db.Customer // where c.Id == 1 // select c).FirstOrDefault(); //var orderInfoList = customer.OrderInfo; ////遍历所有 //while (orderInfoList.Count > 0) //{ // var orderInfo = orderInfoList.FirstOrDefault(); // db.Entry(orderInfo).State = System.Data.Entity.EntityState.Deleted; //} //db.SaveChanges(); var orderList = from o in db.OrderInfo where o.CustomerId == 4 select o; foreach (var item in orderList) { db.Entry(item).State = System.Data.Entity.EntityState.Deleted; } db.SaveChanges(); }
protected void Button1_Click(object sender, EventArgs e) { Model2Container db = new Model2Container(); Customer customer = new Customer() { CustomerName = "张三", CustomerPwd = "123", SubTime = DateTime.Now }; OrderInfo orderInfo1 = new OrderInfo() { Id = Guid.NewGuid(), OrderNum = "10001", CreateDateTime = DateTime.Now, Customer = customer }; OrderInfo orderInfo2 = new OrderInfo() { Id = Guid.NewGuid(), OrderNum = "10002", CreateDateTime = DateTime.Now, Customer = customer }; db.Customer.Add(customer); db.OrderInfo.Add(orderInfo1); db.OrderInfo.Add(orderInfo2); db.SaveChanges(); }