public static Shipper GetShipperForInsert(bool complexScenario = false)
		{
			Shipper shipper = new Shipper();

			shipper.Id = 0;
			shipper.CompanyName = "Trains R Us";
			shipper.Phone = "555-TRAINS";

			if (complexScenario)
			{
				ShipperContact contact = new ShipperContact();
				contact.EmailAddress = "*****@*****.**";
				contact.PhoneNumber = "0123456789";

				shipper.ShipperContacts.Add(contact);
			}

			return shipper;
		}
Exemplo n.º 2
0
        public void LinqToSqlInheritance07()
        {
            Console.WriteLine("****** Before Insert Record ******");
            var ShipperContacts = from sc in db.Contacts.OfType<ShipperContact>()
                                     where sc.CompanyName == "Northwind Shipper"
                                     select sc;

            Console.WriteLine();
            Console.WriteLine("There is {0} Shipper Contact matched our requirement", ShipperContacts.Count());

            ShipperContact nsc = new ShipperContact() { CompanyName = "Northwind Shipper", Phone = "(123)-456-7890" };
            db.Contacts.InsertOnSubmit(nsc);
            db.SubmitChanges();

            Console.WriteLine();
            Console.WriteLine("****** After Insert Record ******");
            ShipperContacts = from sc in db.Contacts.OfType<ShipperContact>()
                               where sc.CompanyName == "Northwind Shipper"
                               select sc;

            Console.WriteLine();
            Console.WriteLine("There is {0} Shipper Contact matched our requirement", ShipperContacts.Count());
            db.Contacts.DeleteOnSubmit(nsc);
            db.SubmitChanges();

        }
Exemplo n.º 3
0
        public static string AddEditShipper(ShipperVm shipperVm)
        {
            int    carrId            = shipperVm.ShipperId;
            string isSaved           = "true";
            EasyFreightEntities   db = new EasyFreightEntities();
            Shipper               carrDb;
            List <ShipperContact> dbContactList = new List <ShipperContact>();

            if (carrId == 0) //Add new case
            {
                carrDb = new Shipper();
            }
            else
            {
                carrDb = db.Shippers.Include("ShipperContacts").Where(x => x.ShipperId == carrId).FirstOrDefault();
                //delete any removed contact on the screen
                dbContactList = carrDb.ShipperContacts.ToList();

                try
                {
                    //Get contact Ids sent from the screen
                    List <int> contactVmIds = shipperVm.ContactPersons.Select(x => x.ContactId).ToList();
                    var        contactDel   = dbContactList.Where(x => !contactVmIds.Contains(x.ContactId)).ToList();

                    foreach (var item in contactDel)
                    {
                        db.ShipperContacts.Remove(item);
                    }
                }
                catch { }
            }

            Mapper.CreateMap <ShipperVm, Shipper>().IgnoreAllNonExisting();
            Mapper.Map(shipperVm, carrDb);



            if (carrId == 0)
            {
                Random rand = new Random();
                carrDb.ShipperCode = rand.Next(10000).ToString();
                db.Shippers.Add(carrDb);
            }

            Mapper.CreateMap <ContactPersonVm, ShipperContact>().IgnoreAllNonExisting()
            .ForMember(x => x.ShipperId, opts => opts.MapFrom(scr => scr.FkValue));

            ShipperContact carrContactDb;

            if (shipperVm.ContactPersons != null && shipperVm.ContactPersons.Count > 0)
            {
                foreach (var item in shipperVm.ContactPersons.Where(x => !string.IsNullOrEmpty(x.ContactName)))
                {
                    if (item.ContactId == 0)
                    {
                        carrContactDb = new ShipperContact();
                    }
                    else
                    {
                        int contVmId = item.ContactId;
                        carrContactDb = dbContactList.Where(x => x.ContactId == contVmId).FirstOrDefault();
                    }

                    Mapper.Map(item, carrContactDb);

                    if (item.ContactId == 0)
                    {
                        carrDb.ShipperContacts.Add(carrContactDb);
                    }
                }
            }
            try
            {
                db.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                isSaved = "false " + e.Message;
            }
            catch (Exception e)
            {
                isSaved = "false " + e.InnerException.InnerException;
            }

            return(isSaved);
        }