internal void insertReceipt(Receipt receip, Customer customer) { using (BSPetShopEntities context = new BSPetShopEntities()) { var customerWithID = from c in context.CustomerSet where c.PhoneNumber.Equals(customer.phonenumber) select c; context.ReceiptSet.Add(new ReceiptSet() { Totalprice = receip.totalPrice().ToString(), Date = receip.date, Customer_Id = customerWithID.Single().Id }); context.SaveChanges(); foreach (Animal a in receip.animals) { Type birdType = typeof(Bird); Type fishType = typeof(Fish); var receipt = from r in context.ReceiptSet where r.Date == receip.date && r.Customer_Id == customer.id select r; if (a.GetType() == birdType) { var query = from b in context.BirdSet where b.Id == a.id select b; foreach (BirdSet b in query) { b.Receipt_Id = receipt.Single().Id; } context.SaveChanges(); } else { var queryF = from f in context.FishSet where f.Id == a.id select f; foreach (FishSet f in queryF) { f.Receipt_Id = receipt.Single().Id; } context.SaveChanges(); } } } }