예제 #1
0
        public ProviderInfo()
        {
            Name = "NHibernate Provider";
            Description = "A Provider that allows access to certain NHibernate-specific settings.";

            NhConfigFile = new NHConfigFile();
        }
예제 #2
0
        private void textBoxConfigFileLocation_TextChanged(object sender, System.EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxConfigFileLocation.Text))
            {
                highlighter1.SetHighlightColor(textBoxConfigFileLocation, DevComponents.DotNetBar.Validator.eHighlightColor.None);
                nhConfigFile = null;
                return;
            }
            if (!File.Exists(textBoxConfigFileLocation.Text))
            {
                highlighter1.SetHighlightColor(textBoxConfigFileLocation, DevComponents.DotNetBar.Validator.eHighlightColor.Orange);
                MessageBox.Show(this, "NHibernate config file doesn't exist. Please select a valid file.", "File missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            nhConfigFile = GetNhConfigFile(textBoxConfigFileLocation.Text);

            if (nhConfigFile == null)
            {
                highlighter1.SetHighlightColor(textBoxConfigFileLocation, DevComponents.DotNetBar.Validator.eHighlightColor.Orange);
                MessageBox.Show(this, "This file is not a valid NHibernate config file. It needs to be an XML file with a <hibernate-configuration> element.", "Invalid file", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                highlighter1.SetHighlightColor(textBoxConfigFileLocation, DevComponents.DotNetBar.Validator.eHighlightColor.None);
            }
        }
예제 #3
0
        private NHConfigFile GetNhConfigFile(string filepath)
        {
            nhConfigFile = null;

            if (!File.Exists(filepath))
            {
                return(null);
            }

            try
            {
                nhConfigFile = ProjectLoader.GetNhConfigFile(filepath);
            }
            catch
            {
                // Do nothing. We are probably trying to process a non-XML file.
            }
            return(nhConfigFile);
        }
예제 #4
0
        private void tbProjectLocation_TextChanged(object sender, System.EventArgs e)
        {
            nhConfigFile = null;
            panelConfigSettings.Visible       = false;
            radioFile.Checked                 = false;
            radioManual.Checked               = false;
            labelConfigFile.Visible           = false;
            textBoxConfigFileLocation.Visible = false;
            buttonBrowse.Visible              = false;
            superTabControl1.Visible          = false;

            if (ValidateProjectLocation())
            {
                highlighter1.SetHighlightColor(tbProjectLocation, DevComponents.DotNetBar.Validator.eHighlightColor.None);

                if (NHibernateHelper.ProjectLoader.IsFluentProject(tbProjectLocation.Text) && !FluentCompiledAssemblyFound())
                {
                    MessageBox.Show(this, "Compiled assembly not found for this Fluent NHibernate project. Please recompile the project, then try again.", "Compiled assembly missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // Check whether we can find a config file
                nhConfigFile = GetNhConfigFileFromCsprojFile(tbProjectLocation.Text);

                if (nhConfigFile == null)
                {
                    MessageBox.Show(this, "No NHibernate config file could be found for this project. If the file exists in another project then please locate it, otherwise manually enter the settings.", "NH config file missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    radioFile.Visible   = true;
                    radioManual.Visible = true;
                }
                else
                {
                    radioFile.Visible   = false;
                    radioManual.Visible = false;
                }
                panelConfigSettings.Visible = nhConfigFile == null;
            }
            else
            {
                highlighter1.SetHighlightColor(tbProjectLocation, DevComponents.DotNetBar.Validator.eHighlightColor.Orange);
            }
        }
예제 #5
0
        private NHConfigFile GetNhConfigFileFromCsprojFile(string filepath)
        {
            nhConfigFile = null;

            if (!File.Exists(filepath))
            {
                return(null);
            }

            try
            {
                // Check whether we can find a config file
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(File.ReadAllText(filepath));
                CSProjFile csProjFile = new CSProjFile(doc, tbProjectLocation.Text);
                Slyce.Common.FileController fileController = new FileController();
                nhConfigFile = ProjectLoader.GetNhConfigFile(csProjFile, fileController);
            }
            catch
            {
                // Do nothing. We are probably trying to process a non-XML file.
            }
            return(nhConfigFile);
        }
예제 #6
0
        private void tbProjectLocation_TextChanged(object sender, System.EventArgs e)
        {
            nhConfigFile = null;
            panelConfigSettings.Visible = false;
            radioFile.Checked = false;
            radioManual.Checked = false;
            labelConfigFile.Visible = false;
            textBoxConfigFileLocation.Visible = false;
            buttonBrowse.Visible = false;
            superTabControl1.Visible = false;

            if (ValidateProjectLocation())
            {
                highlighter1.SetHighlightColor(tbProjectLocation, DevComponents.DotNetBar.Validator.eHighlightColor.None);

                if (NHibernateHelper.ProjectLoader.IsFluentProject(tbProjectLocation.Text) && !FluentCompiledAssemblyFound())
                {
                    MessageBox.Show(this, "Compiled assembly not found for this Fluent NHibernate project. Please recompile the project, then try again.", "Compiled assembly missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // Check whether we can find a config file
                nhConfigFile = GetNhConfigFileFromCsprojFile(tbProjectLocation.Text);

                if (nhConfigFile == null)
                {
                    MessageBox.Show(this, "No NHibernate config file could be found for this project. If the file exists in another project then please locate it, otherwise manually enter the settings.", "NH config file missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    radioFile.Visible = true;
                    radioManual.Visible = true;
                }
                else
                {
                    radioFile.Visible = false;
                    radioManual.Visible = false;
                }
                panelConfigSettings.Visible = nhConfigFile == null;
            }
            else
                highlighter1.SetHighlightColor(tbProjectLocation, DevComponents.DotNetBar.Validator.eHighlightColor.Orange);
        }
예제 #7
0
        private NHConfigFile GetNhConfigFileFromCsprojFile(string filepath)
        {
            nhConfigFile = null;

            if (!File.Exists(filepath))
                return null;

            try
            {
                // Check whether we can find a config file
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(File.ReadAllText(filepath));
                CSProjFile csProjFile = new CSProjFile(doc, tbProjectLocation.Text);
                Slyce.Common.FileController fileController = new FileController();
                nhConfigFile = ProjectLoader.GetNhConfigFile(csProjFile, fileController);
            }
            catch
            {
                // Do nothing. We are probably trying to process a non-XML file.
            }
            return nhConfigFile;
        }
예제 #8
0
        private NHConfigFile GetNhConfigFile(string filepath)
        {
            nhConfigFile = null;

            if (!File.Exists(filepath))
                return null;

            try
            {
                nhConfigFile = ProjectLoader.GetNhConfigFile(filepath);
            }
            catch
            {
                // Do nothing. We are probably trying to process a non-XML file.
            }
            return nhConfigFile;
        }
예제 #9
0
        private NHConfigFile CreateConfigFileManually(ArchAngel.Providers.EntityModel.Controller.DatabaseLayer.DatabaseTypes databaseType, string nhConnectionString)
        {
            NHConfigFile nhFile = new NHConfigFile();

            StringBuilder sb = new StringBuilder(1000);
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            sb.AppendLine("<hibernate-configuration xmlns=\"urn:nhibernate-configuration-2.2\">");
            sb.AppendLine("<session-factory>");

            sb.AppendFormat(@"
                                        <property name=""connection.provider"">NHibernate.Connection.DriverConnectionProvider</property>
                                        <property name=""dialect"">{0}</property>
                                        <property name=""connection.driver_class"">{1}</property>
                                        <property name=""connection.connection_string"">{2}</property>",
                        NHConfigFile.GetDialect(false, databaseType),
                        NHConfigFile.GetDriver(databaseType),
                        nhConnectionString
                        );

            if (!string.IsNullOrWhiteSpace(textBoxCacheProviderClass.Text))
                sb.AppendFormat("<property name=\"cache_provider_class\">{0}</property>{1}", textBoxCacheProviderClass.Text, Environment.NewLine);

            if (!string.IsNullOrWhiteSpace(textBoxCacheQueryCacheFactory.Text))
                sb.AppendFormat("<property name=\"cache_query_cache_factory\">{0}</property>{1}", textBoxCacheQueryCacheFactory.Text, Environment.NewLine);

            if (!string.IsNullOrWhiteSpace(textBoxCacheRegionPrefix.Text))
                sb.AppendFormat("<property name=\"cache_region_prefix\">{0}</property>{1}", textBoxCacheRegionPrefix.Text, Environment.NewLine);

            if (!string.IsNullOrWhiteSpace(textBoxMaxFetchDepth.Text))
                sb.AppendFormat("<property name=\"max_fetch_depth\">{0}</property>{1}", textBoxMaxFetchDepth.Text, Environment.NewLine);

            if (!string.IsNullOrWhiteSpace(textBoxQuerySubstitutions.Text))
                sb.AppendFormat("<property name=\"query_substitutions\">{0}</property>{1}", textBoxQuerySubstitutions.Text, Environment.NewLine);

            if (!string.IsNullOrWhiteSpace(textBoxTransactionFactoryClass.Text))
                sb.AppendFormat("<property name=\"transaction_factory_class\">{0}</property>{1}", textBoxTransactionFactoryClass.Text, Environment.NewLine);

            sb.AppendFormat("<property name=\"cache_use_minimal_puts\">{0}</property>{1}", comboBoxCacheUseMinimalPuts.Text, Environment.NewLine);
            sb.AppendFormat("<property name=\"cache_use_query_cache\">{0}</property>{1}", comboBoxCacheUseQueryCache.Text, Environment.NewLine);
            sb.AppendFormat("<property name=\"generate_statistics\">{0}</property>{1}", comboBoxGenerateStatistics.Text, Environment.NewLine);
            sb.AppendFormat("<property name=\"show_sql\">{0}</property>{1}", comboBoxShowSql.Text, Environment.NewLine);
            sb.AppendFormat("<property name=\"use_outer_join\">{0}</property>{1}", comboBoxUseOuterJoin.Text, Environment.NewLine);
            sb.AppendFormat("<property name=\"use_proxy_validator\">{0}</property>{1}", comboBoxUseProxyValidator.Text, Environment.NewLine);

            sb.AppendLine("	</session-factory></hibernate-configuration>");

            nhFile.ConfigXmlFragment = sb.ToString();
            return nhFile;
        }
예제 #10
0
        private NHConfigFile CreateConfigFileManually(ArchAngel.Providers.EntityModel.Controller.DatabaseLayer.DatabaseTypes databaseType, string nhConnectionString)
        {
            NHConfigFile nhFile = new NHConfigFile();

            StringBuilder sb = new StringBuilder(1000);

            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            sb.AppendLine("<hibernate-configuration xmlns=\"urn:nhibernate-configuration-2.2\">");
            sb.AppendLine("<session-factory>");

            sb.AppendFormat(@"
										<property name=""connection.provider"">NHibernate.Connection.DriverConnectionProvider</property>
										<property name=""dialect"">{0}</property>
										<property name=""connection.driver_class"">{1}</property>
										<property name=""connection.connection_string"">{2}</property>"                                        ,
                            NHConfigFile.GetDialect(false, databaseType),
                            NHConfigFile.GetDriver(databaseType),
                            nhConnectionString
                            );

            if (!string.IsNullOrWhiteSpace(textBoxCacheProviderClass.Text))
            {
                sb.AppendFormat("<property name=\"cache_provider_class\">{0}</property>{1}", textBoxCacheProviderClass.Text, Environment.NewLine);
            }

            if (!string.IsNullOrWhiteSpace(textBoxCacheQueryCacheFactory.Text))
            {
                sb.AppendFormat("<property name=\"cache_query_cache_factory\">{0}</property>{1}", textBoxCacheQueryCacheFactory.Text, Environment.NewLine);
            }

            if (!string.IsNullOrWhiteSpace(textBoxCacheRegionPrefix.Text))
            {
                sb.AppendFormat("<property name=\"cache_region_prefix\">{0}</property>{1}", textBoxCacheRegionPrefix.Text, Environment.NewLine);
            }

            if (!string.IsNullOrWhiteSpace(textBoxMaxFetchDepth.Text))
            {
                sb.AppendFormat("<property name=\"max_fetch_depth\">{0}</property>{1}", textBoxMaxFetchDepth.Text, Environment.NewLine);
            }

            if (!string.IsNullOrWhiteSpace(textBoxQuerySubstitutions.Text))
            {
                sb.AppendFormat("<property name=\"query_substitutions\">{0}</property>{1}", textBoxQuerySubstitutions.Text, Environment.NewLine);
            }

            if (!string.IsNullOrWhiteSpace(textBoxTransactionFactoryClass.Text))
            {
                sb.AppendFormat("<property name=\"transaction_factory_class\">{0}</property>{1}", textBoxTransactionFactoryClass.Text, Environment.NewLine);
            }

            sb.AppendFormat("<property name=\"cache_use_minimal_puts\">{0}</property>{1}", comboBoxCacheUseMinimalPuts.Text, Environment.NewLine);
            sb.AppendFormat("<property name=\"cache_use_query_cache\">{0}</property>{1}", comboBoxCacheUseQueryCache.Text, Environment.NewLine);
            sb.AppendFormat("<property name=\"generate_statistics\">{0}</property>{1}", comboBoxGenerateStatistics.Text, Environment.NewLine);
            sb.AppendFormat("<property name=\"show_sql\">{0}</property>{1}", comboBoxShowSql.Text, Environment.NewLine);
            sb.AppendFormat("<property name=\"use_outer_join\">{0}</property>{1}", comboBoxUseOuterJoin.Text, Environment.NewLine);
            sb.AppendFormat("<property name=\"use_proxy_validator\">{0}</property>{1}", comboBoxUseProxyValidator.Text, Environment.NewLine);

            sb.AppendLine("	</session-factory></hibernate-configuration>");

            nhFile.ConfigXmlFragment = sb.ToString();
            return(nhFile);
        }
예제 #11
0
        private static NHConfigFile GetConfigFromXmlFile(ref string cfgXmlFile, ref bool invalidSchemaFound, string configFilePath)
        {
            NHConfigFile nhConfigFile = null;

            using (var streamReader = new StreamReader(configFilePath))
            {
                XDocument document = XDocument.Load(streamReader);

                if (configFilePath.EndsWith(".cfg.xml", StringComparison.OrdinalIgnoreCase))
                {
                    if (Verifier.IsValidConfigFile(document))
                    {
                        cfgXmlFile = configFilePath;
                        nhConfigFile = new NHConfigFile();
                        nhConfigFile.FilePath = cfgXmlFile;
                        nhConfigFile.ConfigLocation = NHConfigFile.ConfigLocations.NHConfigFile;
                    }
                    else
                    {
                        var schemas = NHibernateFileVerifier.GetSchemasInFile(document).Select(x => x.Value);
                        var file = schemas.FirstOrDefault(s => s.Contains("nhibernate-configuration"));
                        if (file != null)
                        {
                            invalidSchemaFound = true;
                            log.WarnFormat("Found an NHibernate Configuration file that uses an unsupported schema");
                        }
                        else
                        {
                            log.WarnFormat("Possible NHibernate configuration file \"{0}\" failed schema validation");
                        }
                    }
                }
                else if (FileHasNHibernateConfig(configFilePath)) //(FileHasNHibernateConfig(document))
                {
                    cfgXmlFile = configFilePath;
                    nhConfigFile = new NHConfigFile();
                    nhConfigFile.FilePath = cfgXmlFile;

                    if (configFilePath.EndsWith("web.config", StringComparison.OrdinalIgnoreCase))
                        nhConfigFile.ConfigLocation = NHConfigFile.ConfigLocations.WebConfigFile;
                    else if (configFilePath.EndsWith(".config", StringComparison.OrdinalIgnoreCase))
                        nhConfigFile.ConfigLocation = NHConfigFile.ConfigLocations.AppConfigFile;
                    else
                        nhConfigFile.ConfigLocation = NHConfigFile.ConfigLocations.Other;
                }
                else
                    return null;
            }
            return nhConfigFile;
        }
예제 #12
0
        private void buttonNext_Click(object sender, System.EventArgs e)
        {
            if (ValidateProjectLocation())
            {
                if (NHibernateHelper.ProjectLoader.IsFluentProject(tbProjectLocation.Text) &&
                    !FluentCompiledAssemblyFound())
                {
                    MessageBox.Show(this, "Compiled assembly not found for this Fluent NHibernate project. Please recompile the project, then try again.", "Compiled assembly missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (radioManual.Checked)
                {
                    if (ucDatabaseInformation1.ConnectionStringHelper.CurrentDbType == DatabaseTypes.Unknown)
                    {
                        MessageBox.Show(this, "Please select a database.", "Missing data", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    bool informationFilled = LoadExistingDatabase.TestConnection(false, ucDatabaseInformation1);

                    if (informationFilled == false)
                    {
                        return;
                    }

                    nhConfigFile = CreateConfigFileManually(ucDatabaseInformation1.SelectedDatabaseType, ucDatabaseInformation1.ConnectionStringHelper.GetNHConnectionStringSqlClient());
                }
                if (nhConfigFile == null)
                {
                    //MessageBox.Show(this, "No config file specified.", "Config file missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    // Check whether we can find a config file
                    nhConfigFile = GetNhConfigFileFromCsprojFile(tbProjectLocation.Text);

                    if (nhConfigFile == null)
                    {
                        MessageBox.Show(this, "No NHibernate config file could be found for this project. If the file exists in another project then please locate it, otherwise manually enter the settings.", "NH config file missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        radioFile.Visible   = true;
                        radioManual.Visible = true;
                    }
                    else
                    {
                        radioFile.Visible   = false;
                        radioManual.Visible = false;
                    }
                    panelConfigSettings.Visible = nhConfigFile == null;
                    return;
                }
                var newProjectInfo = new LoadExistingNHibernateProjectInfo();
                newProjectInfo.Filename              = tbProjectLocation.Text;
                newProjectInfo.NhConfigFile          = nhConfigFile;
                NewProjectForm.NewProjectInformation = newProjectInfo;
                NewProjectForm.NewProjectOutputPath  = Path.GetDirectoryName(tbProjectLocation.Text);

                //if (radioManual.Checked)
                //{
                //    NewProjectForm.NewProjectInformation = new LoadExistingDatabaseInfo
                //                                            {
                //                                                DatabaseLoader = DatabasePresenter.CreateDatabaseLoader(ucDatabaseInformation1),
                //                                                ConnStringHelper = ucDatabaseInformation1.GetHelper()
                //                                            };
                //}
                NewProjectForm.SetScreenData(
                    ScreenDataKey,
                    new ScreenData
                {
                    ExistingFilename = tbProjectLocation.Text
                });

                // Skip the Database, SelectSchemaObjects and Prefixes screens.
                NewProjectForm.SkipScreens(3);
                NewProjectForm.UserChosenAction = NewProjectFormActions.NewProject;
                NewProjectForm.Finish();
            }
            else
            {
                highlighter1.SetHighlightColor(tbProjectLocation, DevComponents.DotNetBar.Validator.eHighlightColor.Orange);
                MessageBox.Show(this, "Please select a *.csproj file", "Invalid Visual Studio project file", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        internal static ArchAngel.Interfaces.Scripting.NHibernate.Model.IProject FillScriptModel(
			ArchAngel.Providers.EntityModel.Model.MappingLayer.MappingSet mappingSet,
			Slyce.Common.CSProjFile existingCsProjectFile,
			NHConfigFile nhConfigFile)
        {
            bool topLevelLazy = ArchAngel.Interfaces.SharedData.CurrentProject.GetProjectDefaultCollectionLazy();
            ArchAngel.Interfaces.NHibernateEnums.TopLevelAccessTypes topLevelAccess = ArchAngel.Interfaces.SharedData.CurrentProject.GetProjectDefaultAccess();
            ArchAngel.Interfaces.NHibernateEnums.TopLevelCascadeTypes topLevelCascade = ArchAngel.Interfaces.SharedData.CurrentProject.GetProjectDefaultCascade();
            ArchAngel.Interfaces.NHibernateEnums.TopLevelCollectionCascadeTypes topLevelCollectionCascade = ArchAngel.Interfaces.SharedData.CurrentProject.GetProjectDefaultCollectionCascade();
            bool topLevelInverse = ArchAngel.Interfaces.SharedData.CurrentProject.GetProjectDefaultInverse();

            ArchAngel.Interfaces.Scripting.NHibernate.Model.IProject project = new ArchAngel.Interfaces.Scripting.NHibernate.Model.IProject();
            Dictionary<ArchAngel.Providers.EntityModel.Model.EntityLayer.Entity, ArchAngel.Interfaces.Scripting.NHibernate.Model.IEntity> entityLookups = new Dictionary<Entity, ArchAngel.Interfaces.Scripting.NHibernate.Model.IEntity>();
            Dictionary<ArchAngel.Providers.EntityModel.Model.DatabaseLayer.IColumn, ArchAngel.Interfaces.Scripting.NHibernate.Model.IColumn> columnLookups = new Dictionary<ArchAngel.Providers.EntityModel.Model.DatabaseLayer.IColumn, ArchAngel.Interfaces.Scripting.NHibernate.Model.IColumn>();
            Dictionary<ArchAngel.Providers.EntityModel.Model.DatabaseLayer.ITable, ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable> tableLookups = new Dictionary<ArchAngel.Providers.EntityModel.Model.DatabaseLayer.ITable, ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable>();
            Dictionary<ArchAngel.Providers.EntityModel.Model.DatabaseLayer.ITable, ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable> viewLookups = new Dictionary<ArchAngel.Providers.EntityModel.Model.DatabaseLayer.ITable, ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable>();
            Dictionary<ArchAngel.Providers.EntityModel.Model.EntityLayer.Property, ArchAngel.Interfaces.Scripting.NHibernate.Model.IProperty> propertyLookups = new Dictionary<ArchAngel.Providers.EntityModel.Model.EntityLayer.Property, ArchAngel.Interfaces.Scripting.NHibernate.Model.IProperty>();

            project.ExistingCsProjectFile = existingCsProjectFile;
            project.OverwriteFiles = ArchAngel.Interfaces.SharedData.CurrentProject.ProjectSettings.OverwriteFiles;

            if (nhConfigFile != null)
            {
                project.NHibernateConfig = new ArchAngel.Interfaces.Scripting.NHibernate.Model.INhConfig()
                {
                    ConnectionString = nhConfigFile.GetConnectionString(),
                    Driver = nhConfigFile.GetDriver(),
                    Dialect = nhConfigFile.GetDialect(false),
                    DialectSpatial = nhConfigFile.GetDialect(true),
                    FileExists = nhConfigFile.FileExists,
                    ExistingFilePath = nhConfigFile.FilePath
                };
            }

            #region Add Tables
            foreach (var table in mappingSet.Database.Tables)
            {
                ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable newTable = new ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable(mappingSet.Database.Name)
                   {
                       Name = table.Name,
                       Schema = table.Schema,
                       ScriptObject = table,
                       IsView = false
                   };
                foreach (var column in table.Columns)
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IColumn newColumn = CreateIColumn(column);
                    newTable.Columns.Add(newColumn);
                    columnLookups.Add(column, newColumn);
                }
                foreach (var index in table.Indexes)
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IIndex newIndex = CreateIIndex(index);

                    foreach (var column in index.Columns)
                        newIndex.Columns.Add(newTable.Columns.Single(c => c.Name == column.Name));

                    newTable.Indexes.Add(newIndex);
                }
                var primaryKey = table.Keys.SingleOrDefault(k => k.Keytype == Providers.EntityModel.Helper.DatabaseKeyType.Primary);

                if (primaryKey != null)
                {
                    newTable.PrimaryKey = new Interfaces.Scripting.NHibernate.Model.IKey()
                    {
                        KeyType = Interfaces.Scripting.NHibernate.Model.IKey.KeyTypes.Primary,
                        Name = primaryKey.Name,
                        ReferencedPrimaryKey = null,
                        TableName = newTable.Name,
                        TableSchema = newTable.Schema
                    };
                    foreach (var column in primaryKey.Columns)
                        newTable.PrimaryKey.Columns.Add(newTable.Columns.Single(c => c.Name == column.Name));
                }
                tableLookups.Add(table, newTable);
                project.Tables.Add(newTable);
            }
            #endregion

            #region Add Views
            foreach (var view in mappingSet.Database.Views)
            {
                ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable newView = new ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable(mappingSet.Database.Name)
                {
                    Name = view.Name,
                    Schema = view.Schema,
                    ScriptObject = view,
                    IsView = true
                };
                foreach (var column in view.Columns)
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IColumn newColumn = CreateIColumn(column);
                    newView.Columns.Add(newColumn);
                    columnLookups.Add(column, newColumn);
                }
                viewLookups.Add(view, newView);
                project.Views.Add(newView);
            }
            #endregion

            #region Add Entities
            foreach (var entity in mappingSet.EntitySet.Entities)
            {
                ArchAngel.Interfaces.Scripting.NHibernate.Model.IEntity newEntity = CreateIEntity(entity);
                ArchAngel.Providers.EntityModel.Model.DatabaseLayer.ITable primaryTable = ArchAngel.NHibernateHelper.MappingFiles.Version_2_2.EntityMapper.GetPrimaryTable(entity);

                if (primaryTable != null)
                {
                    if (tableLookups.ContainsKey(primaryTable))
                        newEntity.PrimaryMappedTable = tableLookups[primaryTable];
                    else
                        newEntity.PrimaryMappedTable = viewLookups[primaryTable];
                }
                else
                    newEntity.PrimaryMappedTable = null;

                newEntity.IsMapped = ArchAngel.NHibernateHelper.MappingFiles.Version_2_2.Utility.IsEntityMappedToTables(entity);
                newEntity.IsAbstract = entity.IsAbstract;
                newEntity.Cache = entity.GetCache();
                newEntity.Proxy = entity.GetEntityProxy();
                newEntity.BatchSize = entity.GetEntityBatchSize();
                newEntity.SelectBeforeUpdate = entity.GetEntitySelectBeforeUpdate();

                var entityDefaultLazy = entity.GetEntityLazy();

                if (entityDefaultLazy == EntityLazyTypes.inherit_default)
                {
                    if (topLevelLazy) entityDefaultLazy = EntityLazyTypes.@true;
                    else entityDefaultLazy = EntityLazyTypes.@false;
                }
                newEntity.LazyLoad = entityDefaultLazy == EntityLazyTypes.@true;
                newEntity.OptimisticLock = (ArchAngel.Interfaces.Scripting.NHibernate.Model.OptimisticLocks)Enum.Parse(typeof(ArchAngel.Interfaces.Scripting.NHibernate.Model.OptimisticLocks), entity.GetEntityOptimisticLock().ToString(), true);

                CollectionLazyTypes entityDefaultCollectionLazy;
                CascadeTypes entityDefaultCascade;
                PropertyAccessTypes entityDefaultCollectionAccess;

                GetEntityCollectionDefaults(topLevelLazy, topLevelCascade, topLevelAccess, entity, out entityDefaultCollectionLazy, out entityDefaultCascade, out entityDefaultCollectionAccess);

                if (entity.GetEntityDefaultCascade() == CascadeTypes.inherit_default)
                    newEntity.DefaultCascade = (Interfaces.Scripting.NHibernate.Model.CascadeTypes)Enum.Parse(typeof(Interfaces.Scripting.NHibernate.Model.CascadeTypes), entityDefaultCascade.ToString().Replace("_", ""), true);
                else
                    newEntity.DefaultCascade = (Interfaces.Scripting.NHibernate.Model.CascadeTypes)Enum.Parse(typeof(Interfaces.Scripting.NHibernate.Model.CascadeTypes), entity.GetEntityDefaultCascade().ToString().Replace("_", ""), true);

                #region Discriminator
                if (EntityImpl.DetermineInheritanceTypeWithChildren(entity) == EntityImpl.InheritanceType.TablePerClassHierarchy && entity.Discriminator != null)
                {
                    newEntity.Discriminator = new Interfaces.Scripting.NHibernate.Model.IDiscriminator();

                    if (entity.Discriminator.DiscriminatorType == Enums.DiscriminatorTypes.Column)
                    {
                        IColumn discriminatorColumn = entity.MappedTables().ElementAt(0).Columns.Single(c => c.Name == entity.Discriminator.ColumnName);
                        ArchAngel.Providers.EntityModel.Model.EntityLayer.Property prop = entity.Properties.FirstOrDefault(p => p.MappedColumn() == discriminatorColumn);
                        string csharpType = "";

                        if (prop != null)
                            csharpType = prop.NHibernateType;
                        else
                            csharpType = ArchAngel.Providers.EntityModel.Controller.MappingLayer.OneToOneEntityProcessor.ConvertType(discriminatorColumn);

                        newEntity.Discriminator.DiscriminatorType = Interfaces.Scripting.NHibernate.Model.IDiscriminator.DiscriminatorTypes.Column;
                        newEntity.Discriminator.Column = columnLookups[entity.MappedTables().Single().Columns.Single(c => c.Name == entity.Discriminator.ColumnName)];
                        newEntity.Discriminator.CSharpType = csharpType;
                    }
                    else if (entity.Discriminator.DiscriminatorType == Enums.DiscriminatorTypes.Formula)
                    {
                        newEntity.Discriminator.DiscriminatorType = Interfaces.Scripting.NHibernate.Model.IDiscriminator.DiscriminatorTypes.Formula;
                        newEntity.Discriminator.Formula = entity.Discriminator.Formula;
                        //newEntity.Discriminator.CSharpType = csharpType;
                    }
                }
                #endregion

                if (entity.MappedClass != null)
                {
                    newEntity.MappedClass = new ArchAngel.Interfaces.Scripting.NHibernate.Model.ISourceClass();//entity.MappedClass);
                    newEntity.MappedClass.FilePath = entity.EntitySet.MappingSet.CodeParseResults.GetFilenameForParsedClass(entity.MappedClass);
                }
                EntityImpl.InheritanceType inheritanceType = EntityImpl.DetermineInheritanceTypeWithParent(entity);

                switch (inheritanceType)
                {
                    case EntityImpl.InheritanceType.None:
                        newEntity.InheritanceTypeWithParent = Interfaces.Scripting.NHibernate.Model.IEntity.InheritanceTypes.None;
                        break;
                    case EntityImpl.InheritanceType.TablePerClassHierarchy:
                        newEntity.InheritanceTypeWithParent = Interfaces.Scripting.NHibernate.Model.IEntity.InheritanceTypes.TablePerHierarchy;
                        break;
                    case EntityImpl.InheritanceType.TablePerConcreteClass:
                        newEntity.InheritanceTypeWithParent = Interfaces.Scripting.NHibernate.Model.IEntity.InheritanceTypes.TablePerConcreteClass;
                        break;
                    case EntityImpl.InheritanceType.TablePerSubClass:
                        newEntity.InheritanceTypeWithParent = Interfaces.Scripting.NHibernate.Model.IEntity.InheritanceTypes.TablePerSubClass;
                        break;
                    default:
                        throw new NotImplementedException("InheritanceType not handled yet: " + inheritanceType.ToString());
                }
                entityLookups.Add(entity, newEntity);

                #region Add Components
                foreach (var component in entity.Components.OrderBy(c => c.Name))
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IComponentProperty newComponent = new ArchAngel.Interfaces.Scripting.NHibernate.Model.IComponentProperty()
                        {
                            Name = component.Name,
                            Type = component.Specification.Name,
                            IsSetterPrivate = (bool)SharedData.CurrentProject.GetUserOption("UsePrivateSettersOnProperties") ||
                                                    (bool)component.GetUserOptionValue("Component_UsePrivateSetter"),
                            ScriptObject = component
                        };

                    foreach (var prop in component.Properties)
                    {
                        newComponent.Properties.Add(new ArchAngel.Interfaces.Scripting.NHibernate.Model.IField()
                        {
                            // TODO: do component properties have a SetterIsPrivate user option?
                            //IsSetterPrivate = prop.RepresentedProperty.
                            MappedColumn = columnLookups[prop.MappedColumn()],
                            Name = prop.PropertyName,
                            ScriptObject = prop,
                            Type = prop.RepresentedProperty.Type
                        }
                        );
                    }
                    newEntity.Components.Add(newComponent);
                }
                #endregion

                #region Add Properties
                List<string> propertyNamesFromReferences = entity.DirectedReferences.Where(r => r.FromEndEnabled).Select(d => d.FromName).ToList();
                List<string> propertyNamesFromComponents = entity.Components.Select(c => c.Name).ToList();

                foreach (var property in entity.ConcreteProperties.OrderBy(p => p.Name))
                {
                    if (!entity.ForeignKeyPropertiesToExclude.Contains(property))
                    {
                        ArchAngel.Interfaces.Scripting.NHibernate.Model.IProperty newProperty = ConvertProperty(columnLookups, property, entityDefaultCollectionAccess);
                        newEntity.Properties.Add(newProperty);
                        newProperty.Parent = newEntity;
                        propertyLookups.Add(property, newProperty);
                    }
                }
                foreach (var property in entity.PropertiesHiddenByAbstractParent.OrderBy(p => p.Name))
                {
                    if (!entity.ForeignKeyPropertiesToExclude.Contains(property))
                    {
                        if (newEntity.Properties.Any(p => p.Name == property.Name))
                            continue;

                        ArchAngel.Interfaces.Scripting.NHibernate.Model.IProperty newProperty = ConvertProperty(columnLookups, property, entityDefaultCollectionAccess);
                        newProperty.IsInherited = true;
                        newEntity.Properties.Add(newProperty);
                        newProperty.Parent = newEntity;
                        propertyLookups.Add(property, newProperty);
                    }
                }
                newEntity.Properties = newEntity.Properties.OrderBy(p => p.Name).ToList();
                #endregion

                #region Add Key
                if (entity.Key != null)
                {
                    newEntity.Key = new ArchAngel.Interfaces.Scripting.NHibernate.Model.IEntityKey();
                    newEntity.Key.KeyType = (ArchAngel.Interfaces.Scripting.NHibernate.Model.KeyTypes)Enum.Parse(typeof(ArchAngel.Interfaces.Scripting.NHibernate.Model.KeyTypes), entity.Key.KeyType.ToString(), true);

                    foreach (Property keyProperty in entity.Key.Properties)
                    {
                        ArchAngel.Interfaces.Scripting.NHibernate.Model.IProperty existingProperty = newEntity.Properties.SingleOrDefault(p => p.Name == keyProperty.Name);

                        if (existingProperty != null)
                            newEntity.Key.Properties.Add(existingProperty);
                        else
                            newEntity.Key.Properties.Add(ConvertProperty(columnLookups, keyProperty, entityDefaultCollectionAccess));
                    }
                    //foreach (var prop in newEntity.Properties)
                    //{
                    //    if (prop.IsKeyProperty)
                    //        newEntity.Key.Properties.Add(prop);
                    //}
                }
                #endregion

                #region Add Generator
                newEntity.Generator = new Interfaces.Scripting.NHibernate.Model.IEntityGenerator(entity.Generator.ClassName);

                foreach (var p in entity.Generator.Parameters)
                    newEntity.Generator.Parameters.Add(new Interfaces.Scripting.NHibernate.Model.IEntityGenerator.IParameter(p.Name, p.Value));

                #endregion

                project.Entities.Add(newEntity);
            }

            #region Add Children
            foreach (var entity in mappingSet.EntitySet.Entities)
            {
                ArchAngel.Interfaces.Scripting.NHibernate.Model.IEntity newEntity = entityLookups[entity];

                foreach (var child in entity.Children.OrderBy(c => c.Name))
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IEntity newChild = entityLookups[child];
                    newChild.Parent = entityLookups[entity];

                    if (EntityImpl.DetermineInheritanceTypeWithParent(child) == EntityImpl.InheritanceType.TablePerClassHierarchy)
                        newChild.DiscriminatorValue = child.DiscriminatorValue;

                    newEntity.Children.Add(entityLookups[child]);
                }
            }
            #endregion

            //foreach (var entity in mappingSet.EntitySet.Entities)
            //{
            //    if (entity.Parent != null)
            //        entityLookups[entity].Parent = entityLookups[entity.Parent];
            //}
            #endregion

            #region Add References

            foreach (var entity in mappingSet.EntitySet.Entities)
            {
                CollectionLazyTypes entityDefaultLazy;
                CascadeTypes entityDefaultCascade;
                PropertyAccessTypes entityDefaultAccess;

                GetEntityCollectionDefaults(topLevelLazy, topLevelCascade, topLevelAccess, entity, out entityDefaultLazy, out entityDefaultCascade, out entityDefaultAccess);

                Dictionary<ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable, int> manyToManySameTableProcessingCounts = new Dictionary<ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable, int>();
                Dictionary<string, int> fromProcessedRelationships = new Dictionary<string, int>();
                Dictionary<string, int> toProcessedRelationships = new Dictionary<string, int>();

                foreach (var reference in entity.DirectedReferences.Where(r => r.FromEndEnabled).OrderBy(r => r.FromName))
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IReference newReference = new ArchAngel.Interfaces.Scripting.NHibernate.Model.IReference()
                    {
                        Name = reference.FromName,
                        ToName = reference.ToName,
                        IsSetterPrivate = (bool)SharedData.CurrentProject.GetUserOption("UsePrivateSettersOnProperties") ||
                                    (reference.Entity1IsFromEnd ? (bool)reference.Reference.GetUserOptionValue("End1UsePrivateSetter") : (bool)reference.Reference.GetUserOptionValue("End2UsePrivateSetter")),
                        ToEntity = entityLookups[reference.ToEntity],
                        CollectionType = (ArchAngel.Interfaces.Scripting.NHibernate.Model.CollectionTypes)Enum.Parse(typeof(ArchAngel.Interfaces.Scripting.NHibernate.Model.CollectionTypes), NHCollections.GetAssociationType(reference).ToString(), true),
                        FetchType = (ArchAngel.Interfaces.Scripting.NHibernate.Model.FetchTypes)Enum.Parse(typeof(ArchAngel.Interfaces.Scripting.NHibernate.Model.FetchTypes), NHCollections.GetFetchType(reference).ToString(), true),
                        PreviousNames = reference.OldFromNames.ToList(),
                        Insert = reference.Entity1IsFromEnd ? (bool)reference.Reference.GetUserOptionValue("Reference_End1Insert") : (bool)reference.Reference.GetUserOptionValue("Reference_End2Insert"),
                        Update = reference.Entity1IsFromEnd ? (bool)reference.Reference.GetUserOptionValue("Reference_End1Update") : (bool)reference.Reference.GetUserOptionValue("Reference_End2Update")
                    };
                    #region Handle default values

                    #region Cascade
                    string cascade = reference.Entity1IsFromEnd ? reference.Reference.GetReferenceEnd1Cascade().ToString() : reference.Reference.GetReferenceEnd2Cascade().ToString();

                    if (cascade == CascadeTypes.inherit_default.ToString())
                        newReference.CascadeType = (ArchAngel.Interfaces.Scripting.NHibernate.Model.CascadeTypes)Enum.Parse(typeof(ArchAngel.Interfaces.Scripting.NHibernate.Model.CascadeTypes), entityDefaultCascade.ToString().Replace("_", ""), true);
                    else
                        newReference.CascadeType = (ArchAngel.Interfaces.Scripting.NHibernate.Model.CascadeTypes)Enum.Parse(typeof(ArchAngel.Interfaces.Scripting.NHibernate.Model.CascadeTypes), cascade.Replace("_", ""), true);
                    #endregion

                    #region Lazy
                    string lazy = reference.Entity1IsFromEnd ? reference.Reference.GetReferenceEnd1Lazy().ToString() : reference.Reference.GetReferenceEnd2Lazy().ToString();

                    if (lazy == CollectionLazyTypes.inherit_default.ToString())
                        newReference.LazyType = (ArchAngel.Interfaces.Scripting.NHibernate.Model.LazyTypes)Enum.Parse(typeof(ArchAngel.Interfaces.Scripting.NHibernate.Model.LazyTypes), entityDefaultLazy.ToString(), true);
                    else
                        newReference.LazyType = (ArchAngel.Interfaces.Scripting.NHibernate.Model.LazyTypes)Enum.Parse(typeof(ArchAngel.Interfaces.Scripting.NHibernate.Model.LazyTypes), lazy, true);
                    #endregion

                    #region CollectionCascade
                    string collectionCascade = reference.Entity1IsFromEnd ? reference.Reference.GetReferenceEnd1CollectionCascade().ToString() : reference.Reference.GetReferenceEnd2CollectionCascade().ToString();

                    if (collectionCascade == CollectionCascadeTypes.inherit_default.ToString())
                        newReference.CollectionCascadeType = (ArchAngel.Interfaces.Scripting.NHibernate.Model.CollectionCascadeTypes)Enum.Parse(typeof(ArchAngel.Interfaces.Scripting.NHibernate.Model.CollectionCascadeTypes), topLevelCollectionCascade.ToString().Replace("_", ""), true);
                    else
                        newReference.CollectionCascadeType = (ArchAngel.Interfaces.Scripting.NHibernate.Model.CollectionCascadeTypes)Enum.Parse(typeof(ArchAngel.Interfaces.Scripting.NHibernate.Model.CollectionCascadeTypes), collectionCascade.Replace("_", ""), true);
                    #endregion

                    #region Inverse
                    BooleanInheritedTypes inverse = reference.Entity1IsFromEnd ? (BooleanInheritedTypes)reference.Reference.GetUserOptionValue("End1Inverse") : (BooleanInheritedTypes)reference.Reference.GetUserOptionValue("End2Inverse");

                    if (inverse == BooleanInheritedTypes.inherit_default)
                        newReference.Inverse = topLevelInverse;
                    else
                        newReference.Inverse = inverse == BooleanInheritedTypes.@true;
                    #endregion

                    #region OrderBy
                    Property orderByProperty = null;
                    string orderByColumnName = "";
                    bool orderByIsAsc = true;
                    string orderByClause = "";

                    if (reference.Entity1IsFromEnd)
                    {
                        orderByIsAsc = reference.Reference.GetReferenceEnd1OrderByIsAsc();
                        orderByProperty = reference.Reference.GetReferenceEnd1OrderByProperty();
                    }
                    else
                    {
                        orderByIsAsc = reference.Reference.GetReferenceEnd2OrderByIsAsc();
                        orderByProperty = reference.Reference.GetReferenceEnd1OrderByProperty();
                    }
                    if (orderByProperty != null)
                    {
                        var col = orderByProperty.MappedColumn();

                        if (col != null)
                            orderByColumnName = col.Name;

                        if (orderByColumnName.Length > 0)
                        {
                            if (orderByIsAsc)
                                orderByClause = orderByColumnName;
                            else
                                orderByClause = orderByColumnName + " desc";
                        }
                    }
                    #endregion

                    #endregion

                    if (orderByProperty != null)
                        newReference.OrderByProperty = propertyLookups[orderByProperty];

                    newReference.OrderByIsAsc = orderByIsAsc;

                    if (newReference.CollectionType == ArchAngel.Interfaces.Scripting.NHibernate.Model.CollectionTypes.Map ||
                        newReference.CollectionType == ArchAngel.Interfaces.Scripting.NHibernate.Model.CollectionTypes.IDBag ||
                        newReference.CollectionType == ArchAngel.Interfaces.Scripting.NHibernate.Model.CollectionTypes.List)// ||
                    //newReference.CollectionType == ArchAngel.Interfaces.Scripting.NHibernate.Model.CollectionTypes.Bag)
                    {
                        var indexColumn = NHCollections.GetIndexColumn(reference, ArchAngel.NHibernateHelper.MappingFiles.Version_2_2.EntityMapper.GetPrimaryTable(reference.ToEntity));

                        if (indexColumn != null)
                            newReference.CollectionIndexColumn = columnLookups[indexColumn];
                        else
                            throw new Exception(string.Format("No index column found for the {0} collection between {1} and {2}.", newReference.CollectionType, reference.ToEntity.Name, reference.FromEntity.Name));
                    }
                    ArchAngel.NHibernateHelper.MappingFiles.Version_2_2.ReferenceType refType = ArchAngel.NHibernateHelper.MappingFiles.Version_2_2.ReferenceMapper.DetermineReferenceType(reference.Reference);

                    switch (refType)
                    {
                        case ArchAngel.NHibernateHelper.MappingFiles.Version_2_2.ReferenceType.ManyToMany:
                            newReference.ReferenceType = ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceTypes.ManyToMany;
                            newReference.ManyToManyAssociationTable = tableLookups[reference.Reference.MappedTable()];
                            break;
                        case ArchAngel.NHibernateHelper.MappingFiles.Version_2_2.ReferenceType.ManyToOne:
                            newReference.ReferenceType = reference.FromEndCardinality == Cardinality.One ? ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceTypes.OneToMany : ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceTypes.ManyToOne;
                            break;
                        case ArchAngel.NHibernateHelper.MappingFiles.Version_2_2.ReferenceType.OneToOne:
                            newReference.ReferenceType = ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceTypes.OneToOne;
                            break;
                        case ArchAngel.NHibernateHelper.MappingFiles.Version_2_2.ReferenceType.Unsupported:
                            newReference.ReferenceType = ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceTypes.Unsupported;
                            break;
                        default:
                            newReference.ReferenceType = ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceTypes.Unsupported;
                            break;
                    }
                    ITable referenceMappedTable = reference.Reference.MappedTable();

                    if (referenceMappedTable == null &&
                        (newReference.ReferenceType == ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceTypes.ManyToOne ||
                        newReference.ReferenceType == ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceTypes.OneToOne ||
                        newReference.ReferenceType == ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceTypes.OneToMany))
                    {
                        var directedRelationship = ArchAngel.NHibernateHelper.MappingFiles.Version_2_2.ReferenceMapper.GetDirectedMappedRelationship(entity, reference.Reference);
                        newReference.KeyType = (ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceKeyTypes)Enum.Parse(typeof(ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceKeyTypes), directedRelationship.FromKey.Keytype.ToString(), true);

                        if (directedRelationship.ToTable == directedRelationship.FromTable)
                        {
                            if (directedRelationship.ToKey.Keytype == Providers.EntityModel.Helper.DatabaseKeyType.Primary)
                            {
                                foreach (var column in directedRelationship.FromKey.Columns)
                                    newReference.KeyColumns.Add(columnLookups[column]);

                                foreach (var column in directedRelationship.ToKey.Columns)
                                    newReference.ToKeyColumns.Add(columnLookups[column]);
                            }
                            else
                            {
                                foreach (var column in directedRelationship.ToKey.Columns)
                                    newReference.KeyColumns.Add(columnLookups[column]);

                                foreach (var column in directedRelationship.FromKey.Columns)
                                    newReference.ToKeyColumns.Add(columnLookups[column]);
                            }
                        }
                        else
                        {
                            foreach (var column in directedRelationship.FromKey.Columns)
                                newReference.KeyColumns.Add(columnLookups[column]);

                            foreach (var column in directedRelationship.ToKey.Columns)
                                newReference.ToKeyColumns.Add(columnLookups[column]);
                        }
                    }
                    else if (newReference.ReferenceType == ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceTypes.ManyToMany)
                    {
                        newReference.KeyType = ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceKeyTypes.None;

                        ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable fromPrimaryMappedTable = entityLookups[entity].PrimaryMappedTable;
                        ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable toPrimaryMappedTable = newReference.ToEntity.PrimaryMappedTable;

                        List<ArchAngel.Providers.EntityModel.Model.DatabaseLayer.IColumn> fromInPrimaryKey = new List<IColumn>();
                        List<ArchAngel.Providers.EntityModel.Model.DatabaseLayer.IColumn> toInPrimaryKey = new List<IColumn>();

                        if (fromPrimaryMappedTable == toPrimaryMappedTable)
                        {
                            // This many-to-many relationship is to the same table
                            if (manyToManySameTableProcessingCounts.ContainsKey(toPrimaryMappedTable))
                            {
                                int index = manyToManySameTableProcessingCounts[toPrimaryMappedTable];
                                index++;
                                fromInPrimaryKey.AddRange(referenceMappedTable.Relationships.Where(t => t.PrimaryTable == fromPrimaryMappedTable.ScriptObject).ElementAt(index).PrimaryKey.Columns);
                                toInPrimaryKey.AddRange(referenceMappedTable.Relationships.Where(t => t.PrimaryTable == toPrimaryMappedTable.ScriptObject).ElementAt(index).ForeignKey.Columns);
                                manyToManySameTableProcessingCounts[toPrimaryMappedTable] = index;
                            }
                            else
                            {
                                fromInPrimaryKey.AddRange(referenceMappedTable.Relationships.Where(t => t.PrimaryTable == fromPrimaryMappedTable.ScriptObject).ElementAt(0).PrimaryKey.Columns);
                                toInPrimaryKey.AddRange(referenceMappedTable.Relationships.Where(t => t.PrimaryTable == toPrimaryMappedTable.ScriptObject).ElementAt(0).ForeignKey.Columns);
                                manyToManySameTableProcessingCounts.Add(toPrimaryMappedTable, 0);
                            }
                        }
                        else
                        {
                            //if (referenceMappedTable.Relationships.Where(t => t.PrimaryTable == fromPrimaryMappedTable.ScriptObject).Count() != 1)
                            //    throw new Exception(string.Format("Association table [{0}.{1}] has an unexpected number of relationships to [{2}.{3}]. Please contact [email protected] to help us fix this in Visual NHibernate.", referenceMappedTable.Schema, referenceMappedTable.Name, fromPrimaryMappedTable.Schema, fromPrimaryMappedTable.Name));

                            //if (referenceMappedTable.Relationships.Where(t => t.PrimaryTable == toPrimaryMappedTable.ScriptObject).Count() != 1)
                            //    throw new Exception(string.Format("Association table [{0}.{1}] has an unexpected number of relationships to [{2}.{3}]. Please contact [email protected] to help us fix this in Visual NHibernate.", referenceMappedTable.Schema, referenceMappedTable.Name, toPrimaryMappedTable.Schema, toPrimaryMappedTable.Name));

                            //fromInPrimaryKey = referenceMappedTable.Relationships.Single(t => t.PrimaryTable == fromPrimaryMappedTable.ScriptObject).ForeignKey.Columns;
                            //toInPrimaryKey = referenceMappedTable.Relationships.Single(t => t.PrimaryTable == toPrimaryMappedTable.ScriptObject).ForeignKey.Columns;

                            foreach (var rel in referenceMappedTable.Relationships.Where(t => t.PrimaryTable == fromPrimaryMappedTable.ScriptObject))
                                fromInPrimaryKey.AddRange(rel.ForeignKey.Columns);

                            foreach (var rel in referenceMappedTable.Relationships.Where(t => t.PrimaryTable == toPrimaryMappedTable.ScriptObject))
                                toInPrimaryKey.AddRange(rel.ForeignKey.Columns);

                            //if (reference.Reference.MappedRelationship().PrimaryTable == fromPrimaryMappedTable.ScriptObject)
                            //{
                            //    fromInPrimaryKey = reference.Reference.MappedRelationship().ForeignKey.Columns;
                            //    toInPrimaryKey = reference.Reference.MappedRelationship().PrimaryKey.Columns;
                            //}
                            //else
                            //{
                            //    fromInPrimaryKey = reference.Reference.MappedRelationship().PrimaryKey.Columns;
                            //    toInPrimaryKey = reference.Reference.MappedRelationship().ForeignKey.Columns;
                            //}
                        }
                        foreach (var column in fromInPrimaryKey)
                            newReference.KeyColumns.Add(columnLookups[column]);

                        foreach (var column in toInPrimaryKey)
                            newReference.ToKeyColumns.Add(columnLookups[column]);
                    }
                    else if (referenceMappedTable != null) // Association table, but not many-to-many
                    {
                        newReference.KeyType = ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceKeyTypes.None;

                        ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable fromPrimaryMappedTable = entityLookups[entity].PrimaryMappedTable;
                        ArchAngel.Interfaces.Scripting.NHibernate.Model.ITable toPrimaryMappedTable = newReference.ToEntity.PrimaryMappedTable;

                        IEnumerable<ArchAngel.Providers.EntityModel.Model.DatabaseLayer.IColumn> fromInPrimaryKey = null;
                        IEnumerable<ArchAngel.Providers.EntityModel.Model.DatabaseLayer.IColumn> toInPrimaryKey = null;

                        //if (fromPrimaryMappedTable == toPrimaryMappedTable)
                        //{
                        //    // This many-to-many relationship is to the same table
                        //    if (manyToManySameTableProcessingCounts.ContainsKey(toPrimaryMappedTable))
                        //    {
                        //        int index = manyToManySameTableProcessingCounts[toPrimaryMappedTable];
                        //        index++;
                        //        fromInPrimaryKey = referenceMappedTable.Relationships.Where(t => t.PrimaryTable == fromPrimaryMappedTable.ScriptObject).ElementAt(index).ForeignKey.Columns;
                        //        toInPrimaryKey = referenceMappedTable.Relationships.Where(t => t.PrimaryTable == toPrimaryMappedTable.ScriptObject).ElementAt(index).ForeignKey.Columns;
                        //        manyToManySameTableProcessingCounts[toPrimaryMappedTable] = index;
                        //    }
                        //    else
                        //    {
                        //        fromInPrimaryKey = referenceMappedTable.Relationships.Where(t => t.PrimaryTable == fromPrimaryMappedTable.ScriptObject).ElementAt(0).ForeignKey.Columns;
                        //        toInPrimaryKey = referenceMappedTable.Relationships.Where(t => t.PrimaryTable == toPrimaryMappedTable.ScriptObject).ElementAt(0).ForeignKey.Columns;
                        //        manyToManySameTableProcessingCounts.Add(toPrimaryMappedTable, 0);
                        //    }
                        //}
                        //else
                        //{
                        //if (referenceMappedTable.Relationships.Where(t => t.PrimaryTable == fromPrimaryMappedTable.ScriptObject).Count() != 1)
                        //    throw new Exception(string.Format("Association table [{0}.{1}] has an unexpected number of relationships to [{2}.{3}]. Please contact [email protected] to help us fix this in Visual NHibernate.", referenceMappedTable.Schema, referenceMappedTable.Name, fromPrimaryMappedTable.Schema, fromPrimaryMappedTable.Name));

                        //if (referenceMappedTable.Relationships.Where(t => t.PrimaryTable == toPrimaryMappedTable.ScriptObject).Count() != 1)
                        //    throw new Exception(string.Format("Association table [{0}.{1}] has an unexpected number of relationships to [{2}.{3}]. Please contact [email protected] to help us fix this in Visual NHibernate.", referenceMappedTable.Schema, referenceMappedTable.Name, toPrimaryMappedTable.Schema, toPrimaryMappedTable.Name));

                        var fromRelationships = referenceMappedTable.Relationships.Where(t => t.PrimaryTable == fromPrimaryMappedTable.ScriptObject || t.ForeignTable == fromPrimaryMappedTable.ScriptObject).ToList();
                        var toRelationships = referenceMappedTable.Relationships.Where(t => t.PrimaryTable == toPrimaryMappedTable.ScriptObject || t.ForeignTable == toPrimaryMappedTable.ScriptObject).ToList();
                        string key = entity.Name + ": " + fromPrimaryMappedTable.Schema + fromPrimaryMappedTable.Name + " > " + toPrimaryMappedTable.Schema + toPrimaryMappedTable.Name;

                        if (fromRelationships.Count == 1)
                            fromInPrimaryKey = fromRelationships[0].ForeignKey.Columns;
                        else
                        {
                            //string key = reference.GetHashCode().ToString() + "FROM" + fromPrimaryMappedTable.Schema + fromPrimaryMappedTable.Name;
                            int prevIndex = 0;

                            if (!fromProcessedRelationships.ContainsKey(key))
                            {
                                fromProcessedRelationships.Add(key, 0);
                                fromInPrimaryKey = fromRelationships[0].ForeignKey.Columns;
                            }
                            else
                            {
                                prevIndex = fromProcessedRelationships[key];
                                prevIndex++;
                                fromInPrimaryKey = fromRelationships[prevIndex].ForeignKey.Columns;
                                fromProcessedRelationships[key] = prevIndex;
                            }
                        }
                        if (toRelationships.Count == 1)
                            toInPrimaryKey = toRelationships[0].ForeignKey.Columns;
                        else
                        {
                            //string key = reference.GetHashCode().ToString() + "TO" + toPrimaryMappedTable.Schema + toPrimaryMappedTable.Name;
                            int prevIndex = 0;

                            if (!toProcessedRelationships.ContainsKey(key))
                            {
                                toProcessedRelationships.Add(key, 0);
                                toInPrimaryKey = toRelationships[0].ForeignKey.Columns;
                            }
                            else
                            {
                                prevIndex = toProcessedRelationships[key];
                                prevIndex++;
                                toInPrimaryKey = toRelationships[prevIndex].ForeignKey.Columns;
                                toProcessedRelationships[key] = prevIndex;
                            }
                        }

                        //fromInPrimaryKey = referenceMappedTable.Relationships.Single(t => t.PrimaryTable == fromPrimaryMappedTable.ScriptObject || t.ForeignTable == fromPrimaryMappedTable.ScriptObject).ForeignKey.Columns;
                        //toInPrimaryKey = referenceMappedTable.Relationships.Single(t => t.PrimaryTable == toPrimaryMappedTable.ScriptObject || t.ForeignTable == toPrimaryMappedTable.ScriptObject).ForeignKey.Columns;
                        //}
                        foreach (var column in fromInPrimaryKey)
                            newReference.KeyColumns.Add(columnLookups[column]);

                        foreach (var column in toInPrimaryKey)
                            newReference.ToKeyColumns.Add(columnLookups[column]);
                    }
                    else
                        newReference.KeyType = ArchAngel.Interfaces.Scripting.NHibernate.Model.ReferenceKeyTypes.None;

                    if (reference.FromEndCardinality == ArchAngel.Interfaces.Cardinality.Many)
                        newReference.Type = NHCollections.GetCollectionType(reference).Replace(SharedData.CurrentProject.GetUserOption("ProjectNamespace").ToString() + ".Model.", "");
                    else
                        newReference.Type = reference.ToEntity.Name;

                    entityLookups[entity].References.Add(newReference);
                }
            }
            #endregion

            #region Add ComponentSpecs
            foreach (var componentSpec in mappingSet.EntitySet.ComponentSpecifications)
            {
                ArchAngel.Interfaces.Scripting.NHibernate.Model.IComponent newComponentType = new ArchAngel.Interfaces.Scripting.NHibernate.Model.IComponent()
                {
                    Name = componentSpec.Name,
                    ScriptObject = componentSpec
                };

                #region Add fields
                foreach (var field in componentSpec.Properties)
                {
                    ArchAngel.Interfaces.Scripting.NHibernate.Model.IFieldDef newField = new ArchAngel.Interfaces.Scripting.NHibernate.Model.IFieldDef()
                    {
                        Name = field.Name,
                        Type = field.Type,
                        IsSetterPrivate = (bool)SharedData.CurrentProject.GetUserOption("UsePrivateSettersOnProperties") || (bool)field.GetUserOptionValue("ComponentProperty_UsePrivateSetter"),
                        ScriptObject = field
                    };
                    newComponentType.Properties.Add(newField);
                }
                #endregion

                project.Components.Add(newComponentType);
            }
            #endregion

            return project;
        }
예제 #14
0
 public override void Clear()
 {
     _NhConfigFile = new NHConfigFile();
 }
예제 #15
0
        private void textBoxConfigFileLocation_TextChanged(object sender, System.EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxConfigFileLocation.Text))
            {
                highlighter1.SetHighlightColor(textBoxConfigFileLocation, DevComponents.DotNetBar.Validator.eHighlightColor.None);
                nhConfigFile = null;
                return;
            }
            if (!File.Exists(textBoxConfigFileLocation.Text))
            {
                highlighter1.SetHighlightColor(textBoxConfigFileLocation, DevComponents.DotNetBar.Validator.eHighlightColor.Orange);
                MessageBox.Show(this, "NHibernate config file doesn't exist. Please select a valid file.", "File missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            nhConfigFile = GetNhConfigFile(textBoxConfigFileLocation.Text);

            if (nhConfigFile == null)
            {
                highlighter1.SetHighlightColor(textBoxConfigFileLocation, DevComponents.DotNetBar.Validator.eHighlightColor.Orange);
                MessageBox.Show(this, "This file is not a valid NHibernate config file. It needs to be an XML file with a <hibernate-configuration> element.", "Invalid file", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
                highlighter1.SetHighlightColor(textBoxConfigFileLocation, DevComponents.DotNetBar.Validator.eHighlightColor.None);
        }
예제 #16
0
        private void buttonNext_Click(object sender, System.EventArgs e)
        {
            if (ValidateProjectLocation())
            {
                if (NHibernateHelper.ProjectLoader.IsFluentProject(tbProjectLocation.Text) &&
                    !FluentCompiledAssemblyFound())
                {
                    MessageBox.Show(this, "Compiled assembly not found for this Fluent NHibernate project. Please recompile the project, then try again.", "Compiled assembly missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (radioManual.Checked)
                {
                    if (ucDatabaseInformation1.ConnectionStringHelper.CurrentDbType == DatabaseTypes.Unknown)
                    {
                        MessageBox.Show(this, "Please select a database.", "Missing data", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    bool informationFilled = LoadExistingDatabase.TestConnection(false, ucDatabaseInformation1);

                    if (informationFilled == false)
                        return;

                    nhConfigFile = CreateConfigFileManually(ucDatabaseInformation1.SelectedDatabaseType, ucDatabaseInformation1.ConnectionStringHelper.GetNHConnectionStringSqlClient());
                }
                if (nhConfigFile == null)
                {
                    //MessageBox.Show(this, "No config file specified.", "Config file missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    // Check whether we can find a config file
                    nhConfigFile = GetNhConfigFileFromCsprojFile(tbProjectLocation.Text);

                    if (nhConfigFile == null)
                    {
                        MessageBox.Show(this, "No NHibernate config file could be found for this project. If the file exists in another project then please locate it, otherwise manually enter the settings.", "NH config file missing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        radioFile.Visible = true;
                        radioManual.Visible = true;
                    }
                    else
                    {
                        radioFile.Visible = false;
                        radioManual.Visible = false;
                    }
                    panelConfigSettings.Visible = nhConfigFile == null;
                    return;
                }
                var newProjectInfo = new LoadExistingNHibernateProjectInfo();
                newProjectInfo.Filename = tbProjectLocation.Text;
                newProjectInfo.NhConfigFile = nhConfigFile;
                NewProjectForm.NewProjectInformation = newProjectInfo;
                NewProjectForm.NewProjectOutputPath = Path.GetDirectoryName(tbProjectLocation.Text);

                //if (radioManual.Checked)
                //{
                //    NewProjectForm.NewProjectInformation = new LoadExistingDatabaseInfo
                //                                            {
                //                                                DatabaseLoader = DatabasePresenter.CreateDatabaseLoader(ucDatabaseInformation1),
                //                                                ConnStringHelper = ucDatabaseInformation1.GetHelper()
                //                                            };
                //}
                NewProjectForm.SetScreenData(
                                                ScreenDataKey,
                                                new ScreenData
                                                {
                                                    ExistingFilename = tbProjectLocation.Text
                                                });

                // Skip the Database, SelectSchemaObjects and Prefixes screens.
                NewProjectForm.SkipScreens(3);
                NewProjectForm.UserChosenAction = NewProjectFormActions.NewProject;
                NewProjectForm.Finish();
            }
            else
            {
                highlighter1.SetHighlightColor(tbProjectLocation, DevComponents.DotNetBar.Validator.eHighlightColor.Orange);
                MessageBox.Show(this, "Please select a *.csproj file", "Invalid Visual Studio project file", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
예제 #17
0
        public LoadResult LoadEntityModelFromCSProj(string csprojFilePath, NHConfigFile nhConfigFile)
        {
            _progress.SetCurrentState("Loading Entities From Visual Studio Project", ProgressState.Normal);

            EntityLoader entityLoader = new EntityLoader(new FileController());

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(fileController.ReadAllText(csprojFilePath));
            CSProjFile csProjFile = new CSProjFile(doc, csprojFilePath);
            var hbmFiles = GetHBMFilesFromCSProj(csProjFile);

            if (IsFluentProject(csProjFile))
            {
                ArchAngel.Interfaces.SharedData.CurrentProject.SetUserOption("UseFluentNHibernate", true);
                string tempFluentPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Visual NHibernate" + Path.DirectorySeparatorChar + "Temp" + Path.DirectorySeparatorChar + "FluentTemp");
                var fluentHbmFiles = GetHBMFilesForFluentFromCSProj(csProjFile, tempFluentPath);
                // Combine the actual HBM files with the ones derived from FluentNH
                hbmFiles = hbmFiles.Union(fluentHbmFiles);
            }
            else
                ArchAngel.Interfaces.SharedData.CurrentProject.SetUserOption("UseFluentNHibernate", false);

            var csFiles = GetCSharpFilesFromCSProj(doc, csprojFilePath);
            var nhvFiles = GetNHVFilesFromCSProj(doc, csprojFilePath);

            //NHConfigFile nhConfigFile = GetNhConfigFile(csProjFile, fileController);

            var databaseConnector = nhConfigFile == null ? null : nhConfigFile.DatabaseConnector;

            //////// GFH
            // We need to fetch ALL tables, because HBM mappings don't include association tables, or at least it's difficult to find them.
            List<SchemaData> tablesToFetch = null;// entityLoader.GetTablesFromHbmFiles(hbmFiles);

            IDatabaseLoader loader = null;
            IDatabase database = null;

            if (databaseConnector != null)
                database = GetDatabase(databaseConnector, out loader, tablesToFetch);

            _progress.SetCurrentState("Parsing your existing Model Project", ProgressState.Normal);
            var parseResults = ParseResults.ParseCSharpFiles(csFiles);

            _progress.SetCurrentState("Loading Mapping Information From NHibernate Mapping Files", ProgressState.Normal);
            var mappingSet = entityLoader.GetEntities(hbmFiles, parseResults, database);
            entityLoader.ApplyConstraints(mappingSet, nhvFiles, parseResults);

            #region Create References

            // Get a set of all Guids for tables that we will want to create references from
            HashSet<Guid> existingTables = new HashSet<Guid>(database.Tables.Select(t => t.InternalIdentifier));

            foreach (var mappedTable in mappingSet.Mappings.Select(m => m.FromTable))
                existingTables.Add(mappedTable.InternalIdentifier);

            HashSet<Guid> processedRelationships = new HashSet<Guid>();
            foreach (var table in database.Tables)
            {
                foreach (var directedRel in table.DirectedRelationships)
                {
                    var relationship = directedRel.Relationship;

                    if (processedRelationships.Contains(relationship.InternalIdentifier))
                        continue; // Skip relationships that have already been handled.
                    if (relationship.MappedReferences().Any())
                        continue; // Skip relationships that have been mapped by the user.
                    if (existingTables.Contains(directedRel.ToTable.InternalIdentifier) == false)
                        continue; // Skip relationships that have tables that have no mapped Entity

                    if (relationship.PrimaryTable.MappedEntities().FirstOrDefault() == null ||
                        relationship.ForeignTable.MappedEntities().FirstOrDefault() == null)
                    {
                        continue;
                    }
                    ArchAngel.Providers.EntityModel.Controller.MappingLayer.MappingProcessor.ProcessRelationshipInternal(mappingSet, relationship, new ArchAngel.Providers.EntityModel.Controller.MappingLayer.OneToOneEntityProcessor());
                    processedRelationships.Add(relationship.InternalIdentifier);
                }
            }
            #endregion

            foreach (var entity in mappingSet.EntitySet.Entities)
                foreach (var reference in entity.References)
                    if (!mappingSet.EntitySet.References.Contains(reference))
                        mappingSet.EntitySet.AddReference(reference);

            LoadResult result = new LoadResult();
            result.MappingSet = mappingSet;
            result.DatabaseLoader = loader;
            result.NhConfigFile = nhConfigFile;
            result.CsProjFile = csProjFile;
            return result;
        }