protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.Droplist_Tables.Items.Add("-Select One-"); var storeGenerator = new EntityStoreSchemaGenerator( "System.Data.SqlClient", ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString, "namespace"); storeGenerator.GenerateStoreMetadata(); var tables = storeGenerator.StoreItemCollection .GetItems<EntityContainer>() .Single() .BaseEntitySets .OfType<EntitySet>() .Where(s => !s.MetadataProperties.Contains("Type") || s.MetadataProperties["Type"].ToString() == "Tables"); foreach (var table in tables) { this.Droplist_Tables.Items.Add(table.Name); } //Add delete button to gridview //GridButtonColumn deleteButton = new GridButtonColumn(); //this.tableGrid.MasterTableView.Columns.Add(deleteButton); //deleteButton.UniqueName = "deleteButton"; //deleteButton.HeaderText = "Delete"; //deleteButton.Text = "Delete"; //deleteButton.ConfirmText = "Are you sure you want to delete this record?"; //deleteButton.ButtonType = GridButtonColumnType.LinkButton; //deleteButton.CommandName = "Delete"; //Add edit button to gridview GridButtonColumn detailsButton = new GridButtonColumn(); this.tableGrid.MasterTableView.Columns.Add(detailsButton); detailsButton.UniqueName = "detailsButton"; detailsButton.HeaderText = "Details"; detailsButton.Text = "Details"; detailsButton.ButtonType = GridButtonColumnType.LinkButton; detailsButton.CommandName = "Details"; } }
public void ReverseEngineerCodeFirst(Project project) { DebugCheck.NotNull(project); try { // Show dialog with SqlClient selected by default var dialogFactory = _package.GetService<IVsDataConnectionDialogFactory>(); var dialog = dialogFactory.CreateConnectionDialog(); dialog.AddAllSources(); dialog.SelectedSource = new Guid("067ea0d9-ba62-43f7-9106-34930c60c528"); var dialogResult = dialog.ShowDialog(connect: true); if (dialogResult != null) { // Find connection string and provider _package.DTE2.StatusBar.Text = Strings.ReverseEngineer_LoadSchema; var connection = (DbConnection)dialogResult.GetLockedProviderObject(); var connectionString = connection.ConnectionString; var providerManager = (IVsDataProviderManager)Package.GetGlobalService(typeof(IVsDataProviderManager)); IVsDataProvider dp; providerManager.Providers.TryGetValue(dialogResult.Provider, out dp); var providerInvariant = (string)dp.GetProperty("InvariantName"); // Load store schema var storeGenerator = new EntityStoreSchemaGenerator(providerInvariant, connectionString, "dbo"); storeGenerator.GenerateForeignKeyProperties = true; var errors = storeGenerator.GenerateStoreMetadata(_storeMetadataFilters).Where(e => e.Severity == EdmSchemaErrorSeverity.Error); errors.HandleErrors(Strings.ReverseEngineer_SchemaError); // Generate default mapping _package.DTE2.StatusBar.Text = Strings.ReverseEngineer_GenerateMapping; 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(); var mappings = new EdmMapping(modelGenerator, storeGenerator.StoreItemCollection); // Find the project to add the code to var vsProject = (VSLangProj.VSProject)project.Object; var projectDirectory = new FileInfo(project.FileName).Directory; var projectNamespace = (string)project.Properties.Item("RootNamespace").Value; var references = vsProject.References.Cast<VSLangProj.Reference>(); if (!references.Any(r => r.Name == "EntityFramework")) { // Add EF References _package.DTE2.StatusBar.Text = Strings.ReverseEngineer_InstallEntityFramework; try { project.InstallPackage("EntityFramework"); } catch (Exception ex) { _package.LogError(Strings.ReverseEngineer_InstallEntityFrameworkError, ex); } } // Generate Entity Classes and Mappings _package.DTE2.StatusBar.Text = Strings.ReverseEngineer_GenerateClasses; var templateProcessor = new TemplateProcessor(project); var modelsNamespace = projectNamespace + ".Models"; var modelsDirectory = Path.Combine(projectDirectory.FullName, "Models"); var mappingNamespace = modelsNamespace + ".Mapping"; var mappingDirectory = Path.Combine(modelsDirectory, "Mapping"); var entityFrameworkVersion = GetEntityFrameworkVersion(references); foreach (var entityType in entityTypes) { // Generate the code file var entityHost = new EfTextTemplateHost { EntityType = entityType, EntityContainer = modelGenerator.EntityContainer, Namespace = modelsNamespace, ModelsNamespace = modelsNamespace, MappingNamespace = mappingNamespace, EntityFrameworkVersion = entityFrameworkVersion, TableSet = mappings.EntityMappings[entityType].Item1, PropertyToColumnMappings = mappings.EntityMappings[entityType].Item2, ManyToManyMappings = mappings.ManyToManyMappings }; var entityContents = templateProcessor.Process(Templates.EntityTemplate, entityHost); var filePath = Path.Combine(modelsDirectory, entityType.Name + entityHost.FileExtension); project.AddNewFile(filePath, entityContents); var mappingHost = new EfTextTemplateHost { EntityType = entityType, EntityContainer = modelGenerator.EntityContainer, Namespace = mappingNamespace, ModelsNamespace = modelsNamespace, MappingNamespace = mappingNamespace, EntityFrameworkVersion = entityFrameworkVersion, TableSet = mappings.EntityMappings[entityType].Item1, PropertyToColumnMappings = mappings.EntityMappings[entityType].Item2, ManyToManyMappings = mappings.ManyToManyMappings }; var mappingContents = templateProcessor.Process(Templates.MappingTemplate, mappingHost); var mappingFilePath = Path.Combine(mappingDirectory, entityType.Name + "Map" + mappingHost.FileExtension); project.AddNewFile(mappingFilePath, mappingContents); } // Generate Context _package.DTE2.StatusBar.Text = Strings.ReverseEngineer_GenerateContext; var contextHost = new EfTextTemplateHost { EntityContainer = modelGenerator.EntityContainer, Namespace = modelsNamespace, ModelsNamespace = modelsNamespace, MappingNamespace = mappingNamespace, EntityFrameworkVersion = entityFrameworkVersion }; var contextContents = templateProcessor.Process(Templates.ContextTemplate, contextHost); var contextFilePath = Path.Combine(modelsDirectory, modelGenerator.EntityContainer.Name + contextHost.FileExtension); var contextItem = project.AddNewFile(contextFilePath, contextContents); AddConnectionStringToConfigFile(project, connectionString, providerInvariant, modelGenerator.EntityContainer.Name); if (contextItem != null) { // Open context class when done _package.DTE2.ItemOperations.OpenFile(contextFilePath); } _package.DTE2.StatusBar.Text = Strings.ReverseEngineer_Complete; } } catch (Exception exception) { _package.LogError(Strings.ReverseEngineer_Error, exception); } }
private static void ModelGen( string connectionString, string provider, string modelName, Version version, bool includeForeignKeys) { IList<EdmSchemaError> ssdlErrors = null; IList<EdmSchemaError> csdlAndMslErrors = null; // generate the SSDL string ssdlNamespace = modelName + "Model.Store"; EntityStoreSchemaGenerator essg = new EntityStoreSchemaGenerator( provider, connectionString, ssdlNamespace); essg.GenerateForeignKeyProperties = includeForeignKeys; ssdlErrors = essg.GenerateStoreMetadata(new List<EntityStoreSchemaFilterEntry>(), version); // detect if there are errors or only warnings from ssdl generation bool hasSsdlErrors = false; bool hasSsdlWarnings = false; if (ssdlErrors != null) { foreach (EdmSchemaError e in ssdlErrors) { if (e.Severity == EdmSchemaErrorSeverity.Error) { hasSsdlErrors = true; } else if (e.Severity == EdmSchemaErrorSeverity.Warning) { hasSsdlWarnings = true; } } } // write out errors & warnings if (hasSsdlErrors && hasSsdlWarnings) { System.Console.WriteLine("Errors occurred during generation:"); WriteErrors(ssdlErrors); } // if there were errors abort. Continue if there were only warnings if (hasSsdlErrors) { return; } // write the SSDL to a string StringWriter ssdl = new StringWriter(); XmlWriter ssdlxw = XmlWriter.Create(ssdl); essg.WriteStoreSchema(ssdlxw); ssdlxw.Flush(); // generate the CSDL string csdlNamespace = modelName + "Model"; string csdlEntityContainerName = modelName + "Entities"; EntityModelSchemaGenerator emsg = new EntityModelSchemaGenerator( essg.EntityContainer, csdlNamespace, csdlEntityContainerName); emsg.GenerateForeignKeyProperties = includeForeignKeys; csdlAndMslErrors = emsg.GenerateMetadata(version); // detect if there are errors or only warnings from csdl/msl generation bool hasCsdlErrors = false; bool hasCsdlWarnings = false; if (csdlAndMslErrors != null) { foreach (EdmSchemaError e in csdlAndMslErrors) { if (e.Severity == EdmSchemaErrorSeverity.Error) { hasCsdlErrors = true; } else if (e.Severity == EdmSchemaErrorSeverity.Warning) { hasCsdlWarnings = true; } } } // write out errors & warnings if (hasCsdlErrors || hasCsdlWarnings) { System.Console.WriteLine("Errors occurred during generation:"); WriteErrors(csdlAndMslErrors); } // if there were errors, abort. Don't abort if there were only warnigns. if (hasCsdlErrors) { return; } // write CSDL to a string StringWriter csdl = new StringWriter(); XmlWriter csdlxw = XmlWriter.Create(csdl); emsg.WriteModelSchema(csdlxw); csdlxw.Flush(); // write MSL to a string StringWriter msl = new StringWriter(); XmlWriter mslxw = XmlWriter.Create(msl); emsg.WriteStorageMapping(mslxw); mslxw.Flush(); // write csdl, ssdl & msl to the EDMX file ToEdmx( csdl.ToString(), ssdl.ToString(), msl.ToString(), new FileInfo( modelName + ".edmx")); }
private static void ModelGen( string connectionString, string provider, string modelName) { IList<EdmSchemaError> ssdlErrors = null; IList<EdmSchemaError> csdlAndMslErrors = null; // generate the SSDL string ssdlNamespace = modelName + "Model.Store"; EntityStoreSchemaGenerator essg = new EntityStoreSchemaGenerator( provider, connectionString, ssdlNamespace); ssdlErrors = essg.GenerateStoreMetadata(); // write out errors if ((ssdlErrors != null && ssdlErrors.Count > 0)) { System.Console.WriteLine("Errors occurred during generation:"); WriteErrors(ssdlErrors); return; } // write the SSDL to a string StringWriter ssdl = new StringWriter(); XmlWriter ssdlxw = XmlWriter.Create(ssdl); essg.WriteStoreSchema(ssdlxw); ssdlxw.Flush(); // generate the CSDL string csdlNamespace = modelName + "Model"; string csdlEntityContainerName = modelName + "Entities"; EntityModelSchemaGenerator emsg = new EntityModelSchemaGenerator( essg.EntityContainer, csdlNamespace, csdlEntityContainerName); csdlAndMslErrors = emsg.GenerateMetadata(); // write out errors if (csdlAndMslErrors != null && csdlAndMslErrors.Count > 0) { System.Console.WriteLine("Errors occurred during generation:"); WriteErrors(csdlAndMslErrors); return; } // write CSDL to a string StringWriter csdl = new StringWriter(); XmlWriter csdlxw = XmlWriter.Create(csdl); emsg.WriteModelSchema(csdlxw); csdlxw.Flush(); // write MSL to a string StringWriter msl = new StringWriter(); XmlWriter mslxw = XmlWriter.Create(msl); emsg.WriteStorageMapping(mslxw); mslxw.Flush(); // write csdl, ssdl & msl to the EDMX file ToEdmx( csdl.ToString(), ssdl.ToString(), msl.ToString(), new FileInfo( modelName + ".edmx")); }
// End Added ErikEJ #region the functions that actually do the interesting things private static void ModelGen( string connectionString, string provider, string modelName, Version version, bool includeForeignKeys, bool pluralize, List<string> tables) { IList<EdmSchemaError> ssdlErrors = null; IList<EdmSchemaError> csdlAndMslErrors = null; // generate the SSDL string ssdlNamespace = modelName + "Model.Store"; EntityStoreSchemaGenerator essg = new EntityStoreSchemaGenerator( provider, connectionString, ssdlNamespace); essg.GenerateForeignKeyProperties = includeForeignKeys; //ErikEJ Filter selected tables var filters = new List<EntityStoreSchemaFilterEntry>(); foreach (var table in tables) { var entry = new EntityStoreSchemaFilterEntry(null, null, table); filters.Add(entry); } ssdlErrors = essg.GenerateStoreMetadata(filters, version); // detect if there are errors or only warnings from ssdl generation bool hasSsdlErrors = false; bool hasSsdlWarnings = false; if (ssdlErrors != null) { foreach (EdmSchemaError e in ssdlErrors) { if (e.Severity == EdmSchemaErrorSeverity.Error) { hasSsdlErrors = true; } else if (e.Severity == EdmSchemaErrorSeverity.Warning) { hasSsdlWarnings = true; } } } // write out errors & warnings if (hasSsdlErrors && hasSsdlWarnings) { //System.Console.WriteLine("Errors occurred during generation:"); WriteErrors(ssdlErrors); } // if there were errors abort. Continue if there were only warnings if (hasSsdlErrors) { return; } // write the SSDL to a string using (StringWriter ssdl = new StringWriter()) { XmlWriter ssdlxw = XmlWriter.Create(ssdl); essg.WriteStoreSchema(ssdlxw); ssdlxw.Flush(); // generate the CSDL string csdlNamespace = modelName + "Model"; string csdlEntityContainerName = modelName + "Entities"; EntityModelSchemaGenerator emsg = new EntityModelSchemaGenerator( essg.EntityContainer, csdlNamespace, csdlEntityContainerName); emsg.GenerateForeignKeyProperties = includeForeignKeys; // Begin Added ErikEJ if (pluralize) emsg.PluralizationService = System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(new System.Globalization.CultureInfo("en-US")); // End Added ErikEJ csdlAndMslErrors = emsg.GenerateMetadata(version); // detect if there are errors or only warnings from csdl/msl generation bool hasCsdlErrors = false; bool hasCsdlWarnings = false; if (csdlAndMslErrors != null) { foreach (EdmSchemaError e in csdlAndMslErrors) { if (e.Severity == EdmSchemaErrorSeverity.Error) { hasCsdlErrors = true; } else if (e.Severity == EdmSchemaErrorSeverity.Warning) { hasCsdlWarnings = true; } } } // write out errors & warnings if (hasCsdlErrors || hasCsdlWarnings) { //System.Console.WriteLine("Errors occurred during generation:"); WriteErrors(csdlAndMslErrors); } // if there were errors, abort. Don't abort if there were only warnigns. if (hasCsdlErrors) { return; } // write CSDL to a string using (StringWriter csdl = new StringWriter()) { XmlWriter csdlxw = XmlWriter.Create(csdl); emsg.WriteModelSchema(csdlxw); csdlxw.Flush(); // write MSL to a string using (StringWriter msl = new StringWriter()) { XmlWriter mslxw = XmlWriter.Create(msl); emsg.WriteStorageMapping(mslxw); mslxw.Flush(); // write csdl, ssdl & msl to the EDMX file ToEdmx( csdl.ToString(), ssdl.ToString(), msl.ToString(), new FileInfo( modelName + ".edmx"), includeForeignKeys, pluralize); } } } }
public static bool ModelGen(string connectionString, string provider, string modelName, Version version, bool includeForeignKeys, out String modelOut, out List<Object> errors) { modelOut = String.Empty; errors = new List<Object>(); IList<EdmSchemaError> ssdlErrors = null; IList<EdmSchemaError> csdlAndMslErrors = null; // generate the SSDL string ssdlNamespace = modelName + "Model.Store"; EntityStoreSchemaGenerator essg = new EntityStoreSchemaGenerator(provider, connectionString, ssdlNamespace); essg.GenerateForeignKeyProperties = includeForeignKeys; ssdlErrors = essg.GenerateStoreMetadata(new List<EntityStoreSchemaFilterEntry>(), version); // detect if there are errors or only warnings from ssdl generation bool hasSsdlErrors = false; bool hasSsdlWarnings = false; if (ssdlErrors != null) { foreach (EdmSchemaError e in ssdlErrors) { if (e.Severity == EdmSchemaErrorSeverity.Error) { hasSsdlErrors = true; } else if (e.Severity == EdmSchemaErrorSeverity.Warning) { hasSsdlWarnings = true; } } } // write out errors & warnings if (hasSsdlErrors && hasSsdlWarnings) { errors.AddRange(ssdlErrors); } // if there were errors abort. Continue if there were only warnings if (hasSsdlErrors) { return true; } // write the SSDL to a string StringWriter ssdl = new StringWriter(); XmlWriter ssdlxw = XmlWriter.Create(ssdl); essg.WriteStoreSchema(ssdlxw); ssdlxw.Flush(); // generate the CSDL string csdlNamespace = modelName + "Model"; string csdlEntityContainerName = modelName + "Entities"; EntityModelSchemaGenerator emsg = new EntityModelSchemaGenerator(essg.EntityContainer, csdlNamespace, csdlEntityContainerName); emsg.GenerateForeignKeyProperties = includeForeignKeys; csdlAndMslErrors = emsg.GenerateMetadata(version); // detect if there are errors or only warnings from csdl/msl generation bool hasCsdlErrors = false; bool hasCsdlWarnings = false; if (csdlAndMslErrors != null) { foreach (EdmSchemaError e in csdlAndMslErrors) { if (e.Severity == EdmSchemaErrorSeverity.Error) { hasCsdlErrors = true; } else if (e.Severity == EdmSchemaErrorSeverity.Warning) { hasCsdlWarnings = true; } } } // write out errors & warnings if (hasCsdlErrors || hasCsdlWarnings) { errors.AddRange(csdlAndMslErrors); } // if there were errors, abort. Don't abort if there were only warnigns. if (hasCsdlErrors) { return true; } // write CSDL to a string StringWriter csdl = new StringWriter(); XmlWriter csdlxw = XmlWriter.Create(csdl); emsg.WriteModelSchema(csdlxw); csdlxw.Flush(); // write MSL to a string StringWriter msl = new StringWriter(); XmlWriter mslxw = XmlWriter.Create(msl); emsg.WriteStorageMapping(mslxw); mslxw.Flush(); // write csdl, ssdl & msl to the EDMX file. A default Designer section will be added modelOut = ToEdmx(csdl.ToString(), ssdl.ToString(), msl.ToString(), String.Empty); return false; }