private void MergeDuplicateRecord(Person newRecord)
        {
            using (var context = new GeneralModelContainer())
            {
                var sourceList = new List<Guid>();

                if (newRecord != null)
                {

                    foreach (var dupePerson in GetUniqRefDuplicates(newRecord.Person_id))
                    {
                        sourceList.AddRange(
                            _sourceDal.FillSourceTableByPersonOrMarriageId2(dupePerson.Person_id)
                                .Select(dp => dp.SourceId)
                                .
                                ToList());

                        newRecord.MergeInto(dupePerson, _sourceDal);
                    }

                    _sourceMappingsDal.WritePersonSources2(newRecord.Person_id, sourceList, 1);

                    context.SaveChanges();
                }

            }
        }
        public static void MergeInto(this Person _person, Person newPerson, SourceDal sourceDal)
        {
            Guid dummyLocation = new Guid("a813a1ff-6093-4924-a7b2-c5d1af6ff699");

            if (_person.SpouseSurname == "")
                _person.SpouseSurname = newPerson.SpouseSurname;

            if (_person.SpouseName == "")
                _person.SpouseName = newPerson.SpouseName;

            if ((_person.ReferenceLocationId == dummyLocation || _person.ReferenceLocationId == Guid.Empty) &&
                newPerson.ReferenceLocationId != dummyLocation && newPerson.ReferenceLocationId != Guid.Empty)
                _person.ReferenceLocationId = newPerson.ReferenceLocationId;

            if ((_person.DeathLocationId == dummyLocation || _person.ReferenceLocationId == Guid.Empty) &&
                newPerson.DeathLocationId != dummyLocation && newPerson.ReferenceLocationId != Guid.Empty)
                _person.DeathLocationId = newPerson.DeathLocationId;

            if ((_person.BirthLocationId == dummyLocation || _person.ReferenceLocationId == Guid.Empty) &&
                newPerson.BirthLocationId != dummyLocation && newPerson.ReferenceLocationId != Guid.Empty)
                _person.BirthLocationId = newPerson.BirthLocationId;

            if (newPerson.ReferenceLocation != "" && _person.ReferenceLocation == "")
                _person.ReferenceLocation = newPerson.ReferenceLocation;

            if (newPerson.ReferenceDateStr != "" && _person.ReferenceDateStr == "")
                _person.ReferenceDateStr = newPerson.ReferenceDateStr;

            if (newPerson.Occupation != "" && _person.Occupation == "")
            {
                if (_person.Occupation.Trim() == "")
                {
                    _person.Occupation = newPerson.Occupation;
                }
                else
                {
                    _person.Occupation += " " + newPerson.Occupation;
                }
            }

            if (newPerson.Notes != "")
            {
                if (_person.Notes.Trim() == "")
                {
                    _person.Notes = newPerson.Notes;
                }
                else
                {
                    _person.Notes += " " + newPerson.Notes;
                }

            }

            if (newPerson.MotherSurname != "" && _person.MotherSurname == "")
                _person.MotherSurname = newPerson.MotherSurname;

            if (newPerson.MotherChristianName != "" && _person.MotherChristianName == "")
                _person.MotherChristianName = newPerson.MotherChristianName;

            if (newPerson.FatherChristianName != "" && _person.FatherChristianName == "")
                _person.FatherChristianName = newPerson.FatherChristianName;

            if (newPerson.FatherOccupation != "" && _person.FatherOccupation == "")
                _person.FatherOccupation = newPerson.FatherOccupation;

            if (newPerson.DeathCounty != "" && _person.DeathCounty == "")
                _person.DeathCounty = newPerson.DeathCounty;

            if (newPerson.DeathDateStr != "" && _person.DeathDateStr == "")
                _person.DeathDateStr = newPerson.DeathDateStr;

            if (newPerson.ReferenceDateInt > 0 && _person.ReferenceDateInt == 0)
                _person.ReferenceDateInt = newPerson.ReferenceDateInt;

            if (newPerson.DeathInt > 0 && _person.DeathInt == 0)
                _person.DeathInt = newPerson.DeathInt;

            if (newPerson.BirthInt > 0 && _person.BirthInt == 0)
                _person.BirthInt = newPerson.BirthInt;

            if (newPerson.BapInt > 0 && _person.BapInt == 0)
                _person.BapInt = newPerson.BapInt;

            if (newPerson.DeathLocation != "" && _person.DeathLocation == "")
                _person.DeathLocation = newPerson.DeathLocation;

            if (newPerson.BirthCounty != "" && _person.BirthCounty == "")
                _person.BirthCounty = newPerson.BirthCounty;

            if (newPerson.BirthDateStr != "" && _person.BirthDateStr == "")
                _person.BirthDateStr = newPerson.BirthDateStr;

            if (newPerson.BirthLocation != "" && _person.BirthLocation == "")
                _person.BirthLocation = newPerson.BirthLocation;

            if (newPerson.BaptismDateStr != "" && _person.BaptismDateStr == "")
                _person.BaptismDateStr = newPerson.BaptismDateStr;

            _person.IsMale = newPerson.IsMale;

            string source = _person.Source + Environment.NewLine + sourceDal.MakeSourceString(newPerson.Person_id);

            // if(source.Length >49)
            _person.Source = "Multiple sources";
            //  else
            //     _person.Source = source;

            int estBYear = 0;
            int estDYear =0;
            bool isEstBYear =false;
            bool isEstDYear = false;

            DateTools.CalcEstDates(_person.BirthInt, _person.BapInt, _person.DeathInt, out estBYear, out estDYear, out isEstBYear, out isEstDYear, _person.FatherChristianName, _person.MotherChristianName);

            _person.EstBirthYearInt = estBYear;
            _person.EstDeathYearInt = estDYear;
            _person.IsEstBirth = isEstBYear;
            _person.IsEstDeath = isEstDYear;
        }
        public Guid Insert(ServicePerson person)
        {
            //todo fix the service person so it cant contain nulls!!

            using (var context = new GeneralModelContainer())
            {

                var personEntity = new Person
                {
                    IsMale = person.IsMale.ToBool(),
                    ChristianName = person.ChristianName ?? "",
                    Surname = person.Surname ?? "",
                    BirthLocation = person.BirthLocation ?? "",
                    BirthDateStr = person.Birth ?? "",
                    BaptismDateStr = person.Baptism ?? "",
                    DeathDateStr = person.Death ?? "",
                    ReferenceDateStr = person.ReferenceDate ?? "",
                    DeathLocation = person.DeathLocation ?? "",
                    FatherChristianName = person.FatherChristianName ?? "",
                    FatherSurname = person.FatherSurname ?? "",
                    MotherChristianName = person.MotherChristianName ?? "",
                    MotherSurname = person.MotherSurname ?? "",
                    Notes = person.Notes ?? "",
                    Source = person.SourceDescription ?? "",
                    BapInt = person.BaptismYear,
                    BirthInt = person.BirthYear,
                    DeathInt = person.DeathYear,
                    ReferenceDateInt = person.ReferenceDate.ParseToValidYear(), // DateTools.GetDateYear(),
                    ReferenceLocation = person.ReferenceLocation ?? "",
                    BirthCounty = person.BirthCounty ?? "",
                    DeathCounty = person.DeathCounty ?? "",
                    Occupation = person.Occupation ?? "",
                    FatherOccupation = person.FatherOccupation ?? "",
                    SpouseName = person.SpouseChristianName ?? "",
                    SpouseSurname = person.SpouseSurname ?? "",
                    UserId = 1,
                    BirthLocationId = person.BirthLocationId.ToGuid(),
                    DeathLocationId = person.DeathLocationId.ToGuid(),
                    ReferenceLocationId = person.ReferenceLocationId.ToGuid(),
                    TotalEvents = 1,
                    EventPriority = 1,
                    UniqueRef = Guid.NewGuid(),
                    EstBirthYearInt = 0,
                    EstDeathYearInt = 0,
                    IsEstBirth = false,
                    IsEstDeath = false,
                    OthersideChristianName = person.OthersideChristianName,
                    OthersideSurname = person.OthersideSurname,
                    OthersideRelationship = person.OthersideRelationship,
                    Person_id = Guid.NewGuid(),
                    DateAdded = DateTime.Today,
                    DateLastEdit = DateTime.Today
                };

                context.Persons.Add(personEntity);

                context.SaveChanges();

                person.PersonId = personEntity.Person_id;
                return personEntity.Person_id;
            }
        }