public ProviderInfo() { Name = "NHibernate Provider"; Description = "A Provider that allows access to certain NHibernate-specific settings."; NhConfigFile = new NHConfigFile(); }
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); }
public static NHConfigFile GetNhConfigFile(CSProjFile csproj, IFileController fileController) { //if (string.IsNullOrEmpty(csprojPath) || !File.Exists(csprojPath)) // return null; NHConfigFile nhConfigFile = null; var configFiles = GetPossibleNHibernateConfigFilesFromCSProj(csproj, fileController); string cfgXmlFile = null; bool invalidSchemaFound = false; foreach (var configFilePath in configFiles) { if (File.Exists(configFilePath)) { // Test if it is an NHibernate file. nhConfigFile = GetConfigFromXmlFile(ref cfgXmlFile, ref invalidSchemaFound, configFilePath); if (nhConfigFile != null) { break; } } } if (cfgXmlFile == null && !(invalidSchemaFound || configFiles.Contains("hibernate.cfg.xml"))) { // We can't find a valid config file in the project - it might exist in // another project, so ask the user to locate it. string filepath = ""; return(null); } if (cfgXmlFile == null) { if (invalidSchemaFound) { throw new NHibernateConfigException("Your NHibernate Configuration file uses an unsupported version of the schema. We only support NHibernate Version 2.2"); } if (configFiles.Contains("hibernate.cfg.xml")) { throw new NHConfigFileMissingException("Your NHibernate configuration file does not validate against the NHibernate Configuration Schema version 2.2"); } System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (var location in configFiles) { sb.AppendLine(location); } string message = string.Format("Could not find the NHibernate configuration file in your project. Locations searched:{0}{1}", Environment.NewLine, sb); return(null); //throw new NHConfigFileMissingException(message); } return(nhConfigFile); }
public static NHConfigFile GetNhConfigFile(string configFilePath) { string cfgXmlFile = null; bool invalidSchemaFound = false; NHConfigFile nhConfigFile = GetConfigFromXmlFile(ref cfgXmlFile, ref invalidSchemaFound, configFilePath); if (invalidSchemaFound) { return(null); } return(nhConfigFile); }
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); }
public override void Clear() { _NhConfigFile = new NHConfigFile(); }