private async void _HandleGenerateCodeCommandExecuted() { if (!CandidateDefinitions.Any(def => string.IsNullOrEmpty(def.TableName))) { EntityGeneratorViewModel.ShowBusy("Generating entities..."); var request = new EntityGenerationRequest { OutputPath = OutputPath, Server = mPayload.Server, Database = mPayload.Database, Username = mPayload.Username, Password = mPayload.Password, EntityDefinitions = CandidateDefinitions.ToArray(), Namespace = Namespace, }; await EntityGenerationManager.GenerateEntities(request); Properties.Settings.Default.LastEntityNames = string.Join("@", request.EntityDefinitions.Select(entity => string.Format("{0}#{1}", entity.TableName, entity.EntityName))); Properties.Settings.Default.LastNamespace = Namespace; Properties.Settings.Default.LastOutputPath = OutputPath; Properties.Settings.Default.Save(); EntityGeneratorViewModel.HideBusy(); MessageBox.Show( @"Generation complete! ", "Entity Generation Complete", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { MessageBox.Show("An entity name is required for all tables. Please define an entity name for the following tables: " + string.Join(",", CandidateDefinitions.Where(def => string.IsNullOrEmpty(def.EntityName)).Select(def => def.TableName))); } }
public 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); } }