/// <summary>
        ///     Initializes a new instance of the <see cref="AddFieldDialog" /> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="selectedEntityId">The selected entity identifier.</param>
        public AddFieldDialog(IPluginSettings settings, long selectedEntityId)
        {
            InitializeComponent( );

            _viewModel = new AddFieldDialogViewModel(settings, selectedEntityId);

            DataContext = _viewModel;
        }
        /// <summary>
        ///     Adds the field click.
        /// </summary>
        private void AddFieldClick( )
        {
            long id;

            if (IsEntityValid(SelectedText, out id))
            {
                AddFieldDialog dialog = new AddFieldDialog(PluginSettings, id);
                var            result = dialog.ShowDialog( );

                if (result == true)
                {
                    AddFieldDialogViewModel vm = dialog.DataContext as AddFieldDialogViewModel;

                    if (vm != null)
                    {
                        if (vm.SelectedField != null && !string.IsNullOrEmpty(vm.SelectedField.TableName))
                        {
                            var field = vm.SelectedField;

                            string commandText = $@"--ReadiMon AddField
DECLARE @contextInfo VARBINARY(128) = CONVERT( VARBINARY(128), 'Entity Browser->Add Field' )
SET CONTEXT_INFO @contextInfo

INSERT INTO {field.TableName} ( EntityId, TenantId, FieldId, Data{( field is DataAliasInfo ? ", Namespace, AliasMarkerId" : string.Empty )} ) VALUES ( @entityId, @tenantId, @fieldId, @data{( field is DataAliasInfo ? ", @nameSpace, @aliasMarkerId" : string.Empty )} )";

                            using (var command = DatabaseManager.CreateCommand(commandText))
                            {
                                try
                                {
                                    DatabaseManager.AddParameter(command, "@entityId", field.EntityId);
                                    DatabaseManager.AddParameter(command, "@tenantId", field.TenantId);
                                    DatabaseManager.AddParameter(command, "@fieldId", field.FieldId);

                                    var aliasField = field as DataAliasInfo;

                                    if (aliasField != null && !string.IsNullOrEmpty(aliasField.Data))
                                    {
                                        string alias;
                                        string nameSpace;

                                        if (aliasField.Data.GetNamespaceAlias(out nameSpace, out alias))
                                        {
                                            DatabaseManager.AddParameter(command, "@data", alias);
                                            DatabaseManager.AddParameter(command, "@nameSpace", nameSpace);
                                            DatabaseManager.AddParameter(command, "@aliasMarkerId", field.FieldId == GetForwardAliasId(field.TenantId) ? 0 : 1);
                                        }
                                    }

                                    var bitField = field as DataBitInfo;

                                    if (bitField != null)
                                    {
                                        DatabaseManager.AddParameter(command, "@data", bitField.Data);
                                    }

                                    var dateTimeField = field as DataDateTimeInfo;

                                    if (dateTimeField != null)
                                    {
                                        DatabaseManager.AddParameter(command, "@data", dateTimeField.Data);
                                    }

                                    var decimalField = field as DataDecimalInfo;

                                    if (decimalField != null)
                                    {
                                        DatabaseManager.AddParameter(command, "@data", decimalField.Data);
                                    }

                                    var guidField = field as DataGuidInfo;

                                    if (guidField != null)
                                    {
                                        DatabaseManager.AddParameter(command, "@data", guidField.Data);
                                    }

                                    var intField = field as DataIntInfo;

                                    if (intField != null)
                                    {
                                        DatabaseManager.AddParameter(command, "@data", intField.Data);
                                    }

                                    var nVarCharField = field as DataNVarCharInfo;

                                    if (nVarCharField != null)
                                    {
                                        DatabaseManager.AddParameter(command, "@data", nVarCharField.Data);
                                    }

                                    var xmlField = field as DataXmlInfo;

                                    if (xmlField != null)
                                    {
                                        DatabaseManager.AddParameter(command, "@data", xmlField.Data);
                                    }

                                    command.ExecuteNonQuery( );
                                }
                                catch (Exception exc)
                                {
                                    MessageBox.Show(exc.Message);
                                }

                                LoadEntity(SelectedText, true);
                            }
                        }
                    }
                }
            }
        }