Пример #1
0
        public void SaveJobLead(JobLead myJobLead)
        {
            //We will do this within a single Context
            using (var ctx = new JobLeadContext())
            {
                //If we have a JobLeadID of 0 (zero), then this is a new job.
                if (myJobLead.JobLeadID == 0)
                {
                    var contextJobLeadEntity = ctx.JobLeads.Include("AgencyBroker").Include("AgencyContact").Include("EmployerBroker").Include("EmployerBroker").Where(s => s.JobLeadID == myJobLead.JobLeadID).FirstOrDefault <JobLead>();

                    if (myJobLead.AgencyBrokerID != 0)
                    {
                        var newBrokerEntity = ctx.Brokers.Where(s => s.BrokerID == myJobLead.AgencyBrokerID).FirstOrDefault <Broker>();
                        myJobLead.AgencyBroker = newBrokerEntity;
                    }

                    if (myJobLead.EmployerBrokerID != 0)
                    {
                        var newBrokerEntity = ctx.Brokers.Where(s => s.BrokerID == myJobLead.EmployerBrokerID).FirstOrDefault <Broker>();
                        myJobLead.EmployerBroker = newBrokerEntity;
                    }

                    if (myJobLead.AgencyContactID != 0)
                    {
                        var newContactEntity = ctx.Contacts.Where(s => s.ContactID == myJobLead.AgencyContactID).FirstOrDefault <Contact>();
                        myJobLead.AgencyContact = newContactEntity;
                    }

                    if (myJobLead.EmployerContactID != 0)
                    {
                        var newContactEntity = ctx.Contacts.Where(s => s.ContactID == myJobLead.EmployerContactID).FirstOrDefault <Contact>();
                        myJobLead.EmployerContact = newContactEntity;
                    }

                    //I AM NOT SURE THIS WORKS PROPERLY YET

                    //Now to iterate through the JobLeadNotes and add in any new ones
                    List <Note> contextNotesList = new List <Note>();
                    foreach (Note thisNote in myJobLead.JobLeadNotes)
                    {
                        //Get the context version of the current note
                        if (thisNote.NoteID != 0)
                        {
                            var newNoteEntity = ctx.Notes.Where(s => s.NoteID == thisNote.NoteID).FirstOrDefault <Note>();
                            contextNotesList.Add(newNoteEntity);
                        }
                    }

                    if (myJobLead.JobLeadNotes.Count() != 0)
                    {
                        myJobLead.JobLeadNotes = contextNotesList;
                    }

                    //END OF CODE I AM UNSURE ABOUT

                    //Add it to the context.
                    ctx.JobLeads.Add((JobLead)myJobLead); //As this requires the form to know what the actual class is, we definitely need to move this out of the form.
                }
                else //Otherwise we update the existing one.
                {
                    //First, get the whole job lead entity
                    var contextJobLeadEntity = ctx.JobLeads.Include("AgencyBroker").Include("AgencyContact").Include("EmployerBroker").Include("EmployerBroker").Where(s => s.JobLeadID == myJobLead.JobLeadID).FirstOrDefault <JobLead>();

                    //Set all the JobLead entity level values.
                    contextJobLeadEntity.Status   = myJobLead.Status;
                    contextJobLeadEntity.JobTitle = myJobLead.JobTitle;
                    contextJobLeadEntity.Source   = myJobLead.Source;
                    contextJobLeadEntity.CVOrApplicationLocation = myJobLead.CVOrApplicationLocation;
                    contextJobLeadEntity.CoverLetterLocation     = myJobLead.CoverLetterLocation;
                    contextJobLeadEntity.Ref_One      = myJobLead.Ref_One;
                    contextJobLeadEntity.Ref_Two      = myJobLead.Ref_Two;
                    contextJobLeadEntity.Ref_Three    = myJobLead.Ref_Three;
                    contextJobLeadEntity.JobLeadImage = myJobLead.JobLeadImage;

                    //Now we attach the AgentBroker, AgentContact, EmployerBroker and EmployerContact sub entities

                    if (myJobLead.AgencyBrokerID != 0)
                    {
                        var newBrokerEntity = ctx.Brokers.Where(s => s.BrokerID == myJobLead.AgencyBrokerID).FirstOrDefault <Broker>();
                        contextJobLeadEntity.AgencyBroker = newBrokerEntity;
                    }

                    if (myJobLead.EmployerBrokerID != 0)
                    {
                        var newBrokerEntity = ctx.Brokers.Where(s => s.BrokerID == myJobLead.EmployerBrokerID).FirstOrDefault <Broker>();
                        contextJobLeadEntity.EmployerBroker = newBrokerEntity;
                    }

                    if (myJobLead.AgencyContactID != 0)
                    {
                        var newContactEntity = ctx.Contacts.Where(s => s.ContactID == myJobLead.AgencyContactID).FirstOrDefault <Contact>();
                        contextJobLeadEntity.AgencyContact = newContactEntity;
                    }

                    if (myJobLead.EmployerContactID != 0)
                    {
                        var newContactEntity = ctx.Contacts.Where(s => s.ContactID == myJobLead.EmployerContactID).FirstOrDefault <Contact>();
                        contextJobLeadEntity.EmployerContact = newContactEntity;
                    }

                    //Now to iterate through the JobLeadNotes and add in any new ones
                    foreach (Note thisNote in myJobLead.JobLeadNotes)
                    {
                        //Get the context version of the current note
                        var newNoteEntity = ctx.Notes.Where(s => s.NoteID == thisNote.NoteID).FirstOrDefault <Note>();

                        //See if it is already attached to the context version of this job lead
                        if (!contextJobLeadEntity.JobLeadNotes.Contains(newNoteEntity))
                        {
                            contextJobLeadEntity.JobLeadNotes.Add(newNoteEntity);
                        }
                    }
                }

                //Finally, we save the changes to the changes made in the context.
                ctx.SaveChanges();
            }
        }
Пример #2
0
        private void btnDoSomething_Click(object sender, EventArgs e)
        {
            #region OldTestDataGeneration
            //using (var ctx = new JobLeadContext())
            //{
            //    ///*
            //    Name2 newName = new Name2();
            //    newName.FirstName = "James";
            //    newName.MiddleName = "Mark";
            //    newName.Surname = "McDonald";
            //    newName.Title = "Dr";

            //    //ctx.Names.Add(newName);

            //    Address2 newAddress = new Address2();
            //    newAddress.BodyText = "Suite 5,\n2 Constitution Street,\nLeith";
            //    newAddress.City = "Edinburgh";
            //    newAddress.Region = "East Lothian";
            //    newAddress.Country = "Scotland";
            //    newAddress.Postcode = "EH6 6JA";

            //    //ctx.Addresses.Add(newAddress);

            //    Contact2 newContact = new Contact2();
            //    newContact.Address = newAddress;
            //    newContact.Name = newName;
            //    newContact.Email = "*****@*****.**";
            //    newContact.Position = "Worker";
            //    newContact.LandLineTelNo = "0131 444 7642";
            //    newContact.MobileTelNo = "0773355667";

            //    Broker2 newBroker = new Broker2();
            //    //newBroker.myContact = newContact;
            //    newBroker.Contacts.Add(newContact);
            //    newBroker.Name = "New Broker Name";

            //    Address2 newAddress2 = new Address2();
            //    newAddress2.BodyText = "4 Kittle Yards,\n2 Causewayside,\nNewington";
            //    newAddress2.City = "Edinburgh";
            //    newAddress2.Region = "East Lothian";
            //    newAddress2.Country = "Scotland";
            //    newAddress2.Postcode = "EH9 1PJ";

            //    newBroker.Address = newAddress2;
            //    newBroker.LandLineTelNo = "0131 668 8010";
            //    newBroker.Website = "WWW.NewBroker.CO.UK";
            //    newBroker.IsAgency = true;

            //    //Test adding a broker to a broker.
            //    Broker2 newBroker2 = new Broker2();
            //    newBroker2.Name = "Broker's Broker1";
            //    newBroker.Brokers.Add(newBroker2);

            //    Broker2 newBroker3 = new Broker2();
            //    newBroker3.Name = "Broker's Broker2";
            //    newBroker.Brokers.Add(newBroker3);

            //    JobLead2 newJobLead = new JobLead2();
            //    newJobLead.AgencyBroker = newBroker;
            //    newJobLead.AgencyContact = newContact;
            //    newJobLead.CoverLetterLocation = @"C:\CoverLetterDir\CoverLetter.doc";
            //    newJobLead.CVOrApplicationLocation = @"C:\CVLocationDir\cv.doc";
            //    newJobLead.Date = DateTime.Now;
            //    newJobLead.Ref_One = "WID001/0034";
            //    newJobLead.Ref_Two = "AgID-23-12345/002";
            //    newJobLead.Source = @"http://www.bbc.co.uk/news";
            //    newJobLead.Status = newJobLead.StatusList[2];

            //    Broker2 newEmployerBroker = new Broker2();
            //    newEmployerBroker.Name = "Employer Broker";
            //    newEmployerBroker.Address = newAddress;
            //    newEmployerBroker.IsAgency = false;

            //    newJobLead.EmployerBroker = newEmployerBroker;

            //    Name2 newName2 = new Name2();
            //    newName2.FirstName = "Edward";
            //    newName2.MiddleName = "Paul";
            //    newName2.Surname = "Innes";
            //    newName2.Title = "Mr";

            //    Contact2 newContact2 = new Contact2();
            //    newContact2.Address = newAddress2;
            //    newContact2.Name = newName2;
            //    newContact2.Email = "*****@*****.**";
            //    newContact2.Position = "Advisor";
            //    newContact2.LandLineTelNo = "0131 222 6238";
            //    newContact2.MobileTelNo = "07766114488";

            //    newJobLead.EmployerContact = newContact2;


            //    //ctx.Names.Add(newName);
            //    //ctx.Addresses.Add(newAddress);
            //    //ctx.Contacts.Add(newContact);
            //    //ctx.Brokers.Add(newBroker);
            //    ctx.JobLeads.Add(newJobLead);


            //    ctx.SaveChanges();
            //    //*/

            //}
            #endregion

            #region NewTestDataGeneration
            //12 Names
            Name name_01 = new Name("Mr", "Andrew", "John", "McDonald");
            Name name_02 = new Name("Professor", "William", "Henry", "Maitland");
            Name name_03 = new Name("Miss", "Fiona", "Shirley", "Edwards");
            Name name_04 = new Name("Mr", "Brian", "Ian", "Jones");
            Name name_05 = new Name("Dr", "Mary", "Edith", "Fforbes");
            Name name_06 = new Name("Miss", "July", "Margret", "Denby");
            Name name_07 = new Name("Mr", "Oliver", "Peter", "Henrikson");
            Name name_08 = new Name("Ms", "Audrey", "Louise", "Monks");
            Name name_09 = new Name("Mr", "Malcolm", "Frank", "Noble");
            Name name_10 = new Name("Miss", "Edith", "Veronica", "Simons");
            Name name_11 = new Name("Dr", "James", "Scott");
            Name name_12 = new Name("Professor", "Kate", "Leslie", "Brown");


            //8 Addresses
            Address address_01 = new Address("38/2 Prince Regent Street,\nLeith", "Edinburgh", "Lothian", "UK", "EH6 4AT");
            Address address_02 = new Address("Suite 5,\n2Commercial Street,\nLeith", "Edinburgh", "Lothan", "Scotland", "EH6 6JA");
            Address address_03 = new Address("4 Kittle Yards,\nCausewayside,\nNewington", "Edinburgh", "Lothian", "Britain", "EH9 1PJ");
            Address address_04 = new Address("23/23a Dalmeny Street", "Edinburgh", "East Lothian", "UK", "EH6 8PJ");
            Address address_05 = new Address("113 West Regent Street", "Glasgow", "Strathclyde", "Scotland", "G2 2RU");
            Address address_06 = new Address("7 Castle Street", "Edinburgh", "Lothian", "UK", "EH2 3AP");
            Address address_07 = new Address("9 Colme Street", "Edinburgh", "West Lothian", "Britain", "EH3 6AA");
            Address address_08 = new Address("17A South Gyle Crescent", "Edinburgh", "Scotland", "Mid Lothian", "EH12 9FL");

            //12 Contacts
            Contact contact_01 = new Contact(name_01, address_01, "Associate", "*****@*****.**", "077739614", "0131 652 7359");
            Contact contact_02 = new Contact(name_02, address_02, "Department Head", "[email protected],uk", "", "0131 391 2519");
            Contact contact_03 = new Contact(name_03, address_03, "Recruiter", "*****@*****.**", "077183962", "0131 724 2975");
            Contact contact_04 = new Contact(name_04, address_04, "Manager", "*****@*****.**", "077163615", "0131 272 4725");
            Contact contact_05 = new Contact(name_05, address_05, "Development Director", "*****@*****.**", "077253951", "0141 263 2748");
            Contact contact_06 = new Contact(name_06, address_06, "HR Director", "*****@*****.**", "077451835", "0131 263 1462");
            Contact contact_07 = new Contact(name_07, address_07, "Placement Officer", "*****@*****.**", "", "");
            Contact contact_08 = new Contact(name_08, address_08, "Consultant", "*****@*****.**", "077253194", "");
            Contact contact_09 = new Contact(name_09, address_04, "Lead Recruiter", "*****@*****.**", "077451736", "0131 272 4720");
            Contact contact_10 = new Contact(name_10, address_07);
            Contact contact_11 = new Contact(name_11, address_04, ".NET Recruiter", "*****@*****.**", "077369643", "0131 272 4722");
            Contact contact_12 = new Contact(name_12, address_05, "Team Leader", "*****@*****.**", "077254951", "0141 263 2724");

            //2 Employer Brokers
            Broker employerBroker_01 = new Broker();
            employerBroker_01.IsAgency      = false;
            employerBroker_01.Name          = "Bleeding Edge Development.";
            employerBroker_01.Address       = address_02;
            employerBroker_01.LandLineTelNo = "0131 391 2500";
            employerBroker_01.Website       = @"www.NewOps.co.uk";
            employerBroker_01.Contacts.Add(contact_02);

            Broker employerBroker_02 = new Broker();
            employerBroker_02.IsAgency      = false;
            employerBroker_02.Name          = "Blue Sky Solutions.";
            employerBroker_02.Address       = address_05;
            employerBroker_02.LandLineTelNo = "0141 263 2700";
            employerBroker_02.Website       = @"www.BlueSkySolutions.com";
            employerBroker_02.Contacts.Add(contact_05);
            employerBroker_02.Contacts.Add(contact_12);

            //4 Agency Brokers
            Broker agencyBroker_01 = new Broker();
            agencyBroker_01.IsAgency      = true;
            agencyBroker_01.Name          = "New Doors.";
            agencyBroker_01.Address       = address_01;
            agencyBroker_01.LandLineTelNo = "0131 652 7300";
            agencyBroker_01.Website       = @"www.NewDoorsLtd.co.uk";
            agencyBroker_01.Contacts.Add(contact_01);

            Broker agencyBroker_02 = new Broker();
            agencyBroker_02.IsAgency      = true;
            agencyBroker_02.Name          = "First Chance.";
            agencyBroker_02.Address       = address_03;
            agencyBroker_02.LandLineTelNo = "0131 724 2900";
            agencyBroker_02.Website       = @"www.FirstChance.co.uk";
            agencyBroker_02.Contacts.Add(contact_03);

            Broker agencyBroker_03 = new Broker();
            agencyBroker_03.IsAgency      = true;
            agencyBroker_03.Name          = "Finnly & Associates.";
            agencyBroker_03.Address       = address_04;
            agencyBroker_03.LandLineTelNo = "0131 272 4700";
            agencyBroker_03.Website       = @"www.FinnlyAndAssociates.com";
            agencyBroker_03.Contacts.Add(contact_04);
            agencyBroker_03.Contacts.Add(contact_09);
            agencyBroker_03.Contacts.Add(contact_11);

            Broker agencyBroker_04 = new Broker();
            agencyBroker_04.IsAgency      = true;
            agencyBroker_04.Name          = "IT Solutions Limited.";
            agencyBroker_04.Address       = address_06;
            agencyBroker_04.LandLineTelNo = "0131 263 1462";
            agencyBroker_04.Website       = @"www.ITSolutionsLtd.co.uk";
            agencyBroker_04.Contacts.Add(contact_06);

            //As we are connecting brokers to brokers, we could not join them until after they have all been created.
            employerBroker_01.Brokers.Add(agencyBroker_01);
            employerBroker_01.Brokers.Add(agencyBroker_03);

            employerBroker_02.Brokers.Add(agencyBroker_02);
            employerBroker_02.Brokers.Add(agencyBroker_03);
            employerBroker_02.Brokers.Add(agencyBroker_04);

            agencyBroker_01.Brokers.Add(employerBroker_01);

            agencyBroker_02.Brokers.Add(employerBroker_02);

            agencyBroker_03.Brokers.Add(employerBroker_01);
            agencyBroker_03.Brokers.Add(employerBroker_02);

            agencyBroker_04.Brokers.Add(employerBroker_02);

            //2 Job Leads
            JobLead jobLead_01 = new JobLead();
            jobLead_01.JobTitle                = ".NET Software Engineer";
            jobLead_01.AgencyBroker            = agencyBroker_01;
            jobLead_01.AgencyContact           = contact_01;
            jobLead_01.EmployerBroker          = employerBroker_01;
            jobLead_01.EmployerContact         = contact_02;
            jobLead_01.CoverLetterLocation     = @"C:\Jobs\Applications\JobLeadOneCover.doc";
            jobLead_01.CVOrApplicationLocation = @"C:\Jobs\CV\CV.Doc";
            jobLead_01.Ref_One   = @"Job01\0023";
            jobLead_01.Ref_Two   = @"ABC-xyz-123";
            jobLead_01.Ref_Three = @"JobOne Ref 3";
            jobLead_01.Source    = @"www.JobsNow.co.uk\002345\abc.aspx";
            jobLead_01.Status    = jobLead_01.StatusList[2];

            JobLead jobLead_02 = new JobLead();
            jobLead_02.JobTitle                = "C# Developer";
            jobLead_02.AgencyBroker            = agencyBroker_03;
            jobLead_02.AgencyContact           = contact_09;
            jobLead_02.EmployerBroker          = employerBroker_02;
            jobLead_02.EmployerContact         = contact_05;
            jobLead_02.CoverLetterLocation     = @"C:\Jobs\Applications\JobLeadTwoCover.doc";
            jobLead_02.CVOrApplicationLocation = @"C:\Jobs\Applications\JobLeadTwoApplication.doc";
            jobLead_02.Ref_One   = @"Job Two Ref One";
            jobLead_02.Ref_Two   = @"JKL\004\TRW\09-34";
            jobLead_02.Ref_Three = @"Job34-0034";
            jobLead_02.Source    = @"www.jobFinder.com\flax0034\job23.aspx";
            jobLead_02.Status    = jobLead_01.StatusList[4];

            using (var ctx = new JobLeadContext())
            {
                ctx.JobLeads.Add(jobLead_01);
                ctx.JobLeads.Add(jobLead_02);

                ctx.SaveChanges();
            }

            /*
             * JobLead currentJobLead;
             *
             * using (var ctx = new JobLeadContext())
             * {
             *  currentJobLead = ctx.JobLeads.Include("AgencyContact").Where(s => s.JobLeadID == 1).FirstOrDefault<JobLead>();
             * }
             *
             * if (currentJobLead != null)
             * {
             *  currentJobLead.JobTitle = "Changed Title";
             *  currentJobLead.AgencyContact = contact_11;
             * }
             *
             * using (var ctx = new JobLeadContext())
             * {
             *  ctx.Entry(currentJobLead).State = System.Data.Entity.EntityState.Modified;
             *
             *  ctx.SaveChanges();
             * }
             */


            #endregion
        }