public void CodeGenerator(string connectionString, string projectNamespace, Action <string> action, List <string> selectedTables) { try { // Find connection string and provider var providerInvariant = "System.Data.SqlClient"; string currentDirectory = System.IO.Directory.GetCurrentDirectory(); using (DbConnection connection = new SqlConnection(connectionString)) { // Load store schema var storeGenerator = new EntityStoreSchemaGenerator(providerInvariant, connectionString, "dbo"); storeGenerator.GenerateForeignKeyProperties = true; var errors = storeGenerator.GenerateStoreMetadata(_storeMetadataFilters).Where(e => e.Severity == EdmSchemaErrorSeverity.Error); // Generate default mapping var contextName = connection.Database.Replace(" ", string.Empty).Replace(".", string.Empty) + "Context"; var modelGenerator = new EntityModelSchemaGenerator(storeGenerator.EntityContainer, "DefaultNamespace", contextName); modelGenerator.PluralizationService = PluralizationService.CreateService(new CultureInfo("en")); modelGenerator.GenerateForeignKeyProperties = true; modelGenerator.GenerateMetadata(); // Pull out info about types to be generated var entityTypes = modelGenerator.EdmItemCollection.OfType <EntityType>().ToArray().ToList(); var mappings = new EdmMapping(modelGenerator, storeGenerator.StoreItemCollection); var templateProcessor = new TemplateProcessor(); entityTypes = HandleCompletedRun(entityTypes, selectedTables); TemplateCommon.projectNamespace = projectNamespace; TemplateCommon.entityFrameworkVersion = GetEntityFrameworkVersion(); TemplateCommon.Init(); foreach (var entityType in entityTypes) { var entityName = entityType.Name.RemoveSpecialChar(); EntityTemplate entityTemplate = new EntityTemplate(entityType); entityTemplate.Generator(modelGenerator, templateProcessor, mappings); // Generate the map file new MappingTemplate(entityType).Generator(modelGenerator, templateProcessor, mappings); //Generate the repository file new RepositoryTemplate(entityType).Generator(modelGenerator, templateProcessor, mappings); //Generate the service file new ServiceTemplate(entityType).Generator(modelGenerator, templateProcessor, mappings); //Generate the svc file new SvcTemplate(entityType).Generator(modelGenerator, templateProcessor, mappings); //Generate the svc test file new SvcTestTemplate(entityType).Generator(modelGenerator, templateProcessor, mappings); } // Generate Context new ContextTemplate().Generator(modelGenerator, templateProcessor, mappings); if (action != null) { action("代码生成成功"); } } } catch (Exception exception) { if (action != null) { action(exception.Message); } } }