private async void _HandleGenerateCodeCommandExecuted()
        {
            if (!CandidateDefinitions.Any(def => string.IsNullOrEmpty(def.TableName)))
            {
                EntityGeneratorViewModel.ShowBusy("Generating entities...");

                var request = new EntityGenerationRequest
                {
                    OutputPath        = OutputPath,
                    Server            = mPayload.Server,
                    Database          = mPayload.Database,
                    Username          = mPayload.Username,
                    Password          = mPayload.Password,
                    EntityDefinitions = CandidateDefinitions.ToArray(),
                    Namespace         = Namespace,
                };

                await EntityGenerationManager.GenerateEntities(request);

                Properties.Settings.Default.LastEntityNames = string.Join("@", request.EntityDefinitions.Select(entity => string.Format("{0}#{1}", entity.TableName, entity.EntityName)));
                Properties.Settings.Default.LastNamespace   = Namespace;
                Properties.Settings.Default.LastOutputPath  = OutputPath;
                Properties.Settings.Default.Save();

                EntityGeneratorViewModel.HideBusy();

                MessageBox.Show(
                    @"Generation complete! ", "Entity Generation Complete", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                MessageBox.Show("An entity name is required for all tables. Please define an entity name for the following tables: " +
                                string.Join(",", CandidateDefinitions.Where(def => string.IsNullOrEmpty(def.EntityName)).Select(def => def.TableName)));
            }
        }
        public EntityEntryViewModel(SelectedTablesPayload payload)
        {
            mPayload = payload;

            CandidateDefinitions = payload.SelectedTables.Select(table => new EntityDefinition()
            {
                TableName  = table,
                EntityName = table + "Entity"
            }).ToArray();

            if (payload.FilePath != null)
            {
                OutputPath = payload.FilePath;
            }

            else if (!string.IsNullOrEmpty(Properties.Settings.Default.LastOutputPath))
            {
                OutputPath = Properties.Settings.Default.LastOutputPath;
            }
            else
            {
                var currentDirectory = Directory.GetCurrentDirectory();
                if (currentDirectory.EndsWith(@"XXX\Tools\CodeGenerators\EntityGenerator\EntityGenerator\bin\Debug"))
                {
                    OutputPath = currentDirectory.Replace(@"XXX\Tools\CodeGenerators\EntityGenerator\EntityGenerator\bin\Debug", @"XXX\Server");
                }
                else
                {
                    OutputPath = currentDirectory;
                }
            }

            if (payload.FilePath == null)
            {
                if (!string.IsNullOrEmpty(Properties.Settings.Default.LastNamespace))
                {
                    Namespace = Properties.Settings.Default.LastNamespace;
                }
                else
                {
                    Namespace = "XXX";
                }
            }

            if (!string.IsNullOrEmpty(Properties.Settings.Default.LastEntityNames))
            {
                var oldPairs = Properties.Settings.Default.LastEntityNames.Split('@');
                foreach (var oldPair in oldPairs)
                {
                    var tablePairs = oldPair.Split('#');
                    var newDef     = CandidateDefinitions.SingleOrDefault(def => def.TableName == tablePairs[0]);
                    if (newDef != null)
                    {
                        newDef.EntityName = tablePairs[1];
                    }
                }
            }
        }