public void OptimizeContext(dynamic context) { Type contextType = context.GetType(); if (GetEntityFrameworkVersion(contextType) >= new Version(6, 0)) { MessageBox.Show( "Generating views for Entity Framework version 6 is currently not supported.", "Entity Framework Power Tools", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { var selectedItem = _package.DTE2.SelectedItems.Item(1); var selectedItemExtension = (string)selectedItem.ProjectItem.Properties.Item("Extension").Value; var languageOption = selectedItemExtension == FileExtensions.CSharp ? LanguageOption.GenerateCSharpCode : LanguageOption.GenerateVBCode; var objectContext = DbContextPackage.GetObjectContext(context); var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace); OptimizeContextCore(languageOption, contextType.Name, mappingCollection); } catch (Exception ex) { _package.LogError(Strings.Optimize_ContextError(contextType.Name), ex); } }
public void ViewDdl(dynamic context) { Type contextType = context.GetType(); try { var filePath = Path.Combine( Path.GetTempPath(), contextType.Name + FileExtensions.Sql); if (File.Exists(filePath)) { File.SetAttributes(filePath, FileAttributes.Normal); } var objectContext = DbContextPackage.GetObjectContext(context); File.WriteAllText(filePath, objectContext.CreateDatabaseScript()); File.SetAttributes(filePath, FileAttributes.ReadOnly); _package.DTE2.ItemOperations.OpenFile(filePath); } catch (Exception exception) { _package.LogError(Strings.ViewDdlError(contextType.Name), exception); } }
private Version GetEntityFrameworkVersion(DbContextPackage package, Project project) { var vsProject = (VSLangProj.VSProject)project.Object; var entityFrameworkReference = vsProject.References.Cast <VSLangProj.Reference>().FirstOrDefault(r => r.Name == "EntityFramework"); if (entityFrameworkReference == null) { // Add EF References package.LogInfo(Strings.ReverseEngineer_InstallEntityFramework); try { project.InstallPackage("EntityFramework"); } catch (Exception ex) { entityFrameworkReference = vsProject.References.Cast <VSLangProj.Reference>().FirstOrDefault(r => r.Name == "EntityFramework"); if (entityFrameworkReference == null) { package.LogError(Strings.ReverseEngineer_InstallEntityFrameworkError, ex); throw new Exception("安装EF出错!"); } } } return(new Version(entityFrameworkReference.Version)); }
public void OptimizeContext(dynamic context) { Type contextType = context.GetType(); try { var selectedItem = _package.DTE2.SelectedItems.Item(1); var selectedItemExtension = (string)selectedItem.ProjectItem.Properties.Item("Extension").Value; var languageOption = selectedItemExtension == FileExtensions.CSharp ? LanguageOption.GenerateCSharpCode : LanguageOption.GenerateVBCode; var objectContext = DbContextPackage.GetObjectContext(context); var baseFileName = contextType.Name; if (GetEntityFrameworkVersion(contextType) < new Version(6, 0)) { var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace); OptimizeContextEF5(languageOption, baseFileName, mappingCollection, selectedItem); } else { var metadataWorkspace = objectContext.MetadataWorkspace; var getItemCollection = ((Type)metadataWorkspace.GetType()).GetMethod("GetItemCollection"); var dataSpace = getItemCollection.GetParameters().First().ParameterType; var mappingCollection = getItemCollection.Invoke( metadataWorkspace, new[] { Enum.Parse(dataSpace, "CSSpace") }); OptimizeContextEF6( languageOption, baseFileName, mappingCollection, selectedItem, contextType.FullName); } } catch (Exception ex) { _package.LogError(Strings.Optimize_ContextError(contextType.Name), ex); } }
public void AddCustomTemplates(Project project) { DebugCheck.NotNull(project); try { AddTemplate(project); } catch (Exception ex) { _package.LogError(Strings.AddTemplatesError, ex); } }
public void ViewContext(MenuCommand menuCommand, dynamic context, Type systemContextType) { DebugCheck.NotNull(menuCommand); DebugCheck.NotNull(systemContextType); Type contextType = context.GetType(); try { var filePath = Path.Combine( Path.GetTempPath(), contextType.Name + (menuCommand.CommandID.ID == PkgCmdIDList.cmdidViewEntityDataModel ? FileExtensions.EntityDataModel : FileExtensions.Xml)); if (File.Exists(filePath)) { File.SetAttributes(filePath, FileAttributes.Normal); } using (var fileStream = File.Create(filePath)) { using (var xmlWriter = XmlWriter.Create(fileStream, new XmlWriterSettings { Indent = true })) { var edmxWriterType = systemContextType.Assembly.GetType("System.Data.Entity.Infrastructure.EdmxWriter"); if (edmxWriterType != null) { edmxWriterType.InvokeMember( "WriteEdmx", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null, new object[] { context, xmlWriter }); } } } _package.DTE2.ItemOperations.OpenFile(filePath); File.SetAttributes(filePath, FileAttributes.ReadOnly); } catch (Exception exception) { _package.LogError(Strings.ViewContextError(contextType.Name), exception); } }
public void AddCustomTemplates(Project project) { DebugCheck.NotNull(project); try { AddTemplate(project, Templates.ContextTemplate); AddTemplate(project, Templates.EntityTemplate); AddTemplate(project, Templates.MappingTemplate); } catch (Exception ex) { _package.LogError(Strings.AddTemplatesError, ex); } }
public void AddCustomTemplates(Project project) { Contract.Requires(project != null); try { AddTemplate(project, Templates.ContextTemplate); AddTemplate(project, Templates.EntityTemplate); AddTemplate(project, Templates.MappingTemplate); } catch (Exception ex) { _package.LogError(Strings.AddTemplatesError, ex); } }