コード例 #1
0
        public void DetermineEntityFile(string outputPath, string expectedOutputPathPrefix, string expectedOutputPath)
        {
            var splitFilePath = FilePathSplitter.DetermineEntityFile(outputPath);

            Assert.That(splitFilePath.EntityOutputPathPrefix, Is.EqualTo(expectedOutputPathPrefix));
            Assert.That(splitFilePath.EntityOutputPath, Is.EqualTo(expectedOutputPath));
        }
コード例 #2
0
        public async Task GenerateEntities(EntityGenerationRequest request)
        {
            var connectionString = string.Format(@"Data Source={0};Initial Catalog={1};User Id={2};Password={3};",
                                                 request.Server, request.Database, request.Username, request.Password);

            var  generator          = CodeFirstGenerator.Load(connectionString, request.EntityDefinitions.ToDictionary(def => def.TableName, def => def.EntityName));
            bool makePartialClasses = false;

            EntityFile entityFile = FilePathSplitter.DetermineEntityFile(request.OutputPath);

            var projectDirectoryPaths = Directory.GetFiles(entityFile.EntityOutputPath, "*.csproj");

            if (projectDirectoryPaths.Length == 1)
            {
                var projectDirectoryPath = projectDirectoryPaths.Single();

                var project = new Project(projectDirectoryPath);

                var serverPath = FilePathSplitter.DetermineServerPath(request.OutputPath);

                foreach (var entityDefinition in request.EntityDefinitions.Where(table => !generator.IsManyToManyMappingTable(table.TableName)))
                {
                    var tryToSave = _SearchForExistingTableName(entityDefinition.TableName, serverPath);

                    if (tryToSave)
                    {
                        var pocoEntity = EntityGeneratorEngine.GeneratePocoEntity(request.Namespace, entityDefinition.TableName, entityDefinition.EntityName, makePartialClasses, generator);
                        _SaveOutput(request.OutputPath, Path.Combine(entityDefinition.EntityName + ".cs"), entityFile.EntityOutputPathPrefix, pocoEntity, project);

                        var mapping = EntityGeneratorEngine.GenerateMapping(request.Namespace + ".Mapping", entityDefinition.TableName, entityDefinition.EntityName, generator);
                        _SaveOutput(request.OutputPath, Path.Combine("Mapping", entityDefinition.EntityName + "Map.cs"), entityFile.EntityOutputPathPrefix, mapping, project);
                    }
                }

                if (project.IsDirty)
                {
                    _SortProjectDirectory(projectDirectoryPath);
                }

                ProjectCollection.GlobalProjectCollection.UnloadProject(project);
            }
        }