Ejemplo n.º 1
0
        public IList <EdmSchemaError> GenerateCode(EFArtifact artifact, string defaultNamespace, TextWriter outputWriter)
        {
            Debug.Assert(outputWriter != null, "OutputWriter parameter is null");
            Debug.Assert(artifact != null, "artifact parameter is null");

            // create a specialized xml reader that piggybacks off of XNodeReader and keeps track
            // of line numbers using the Xml Model
            using (var xmlModelReader = CreateXmlReader(artifact, artifact.ConceptualModel().XElement))
            {
                var codeGenerator = CreateCodeGenerator(_language, _targetEntityFrameworkVersion);

                Debug.Assert(artifact.ConceptualModel() != null, "Artifact ConceptuaModel is null");

                if (defaultNamespace != null && artifact.ConceptualModel() != null)
                {
                    codeGenerator.AddNamespaceMapping(artifact.ConceptualModel().Namespace.Value, defaultNamespace);
                }

                return(codeGenerator.GenerateCode(xmlModelReader, outputWriter));
            }
        }
        public IList<EdmSchemaError> GenerateCode(EFArtifact artifact, string defaultNamespace, TextWriter outputWriter)
        {
            Debug.Assert(outputWriter != null, "OutputWriter parameter is null");
            Debug.Assert(artifact != null, "artifact parameter is null");

            // create a specialized xml reader that piggybacks off of XNodeReader and keeps track 
            // of line numbers using the Xml Model
            using (var xmlModelReader = CreateXmlReader(artifact, artifact.ConceptualModel().XElement))
            {
                var codeGenerator = CreateCodeGenerator(_language, _targetEntityFrameworkVersion);

                Debug.Assert(artifact.ConceptualModel() != null, "Artifact ConceptuaModel is null");

                if (defaultNamespace != null && artifact.ConceptualModel() != null)
                {
                    codeGenerator.AddNamespaceMapping(artifact.ConceptualModel().Namespace.Value, defaultNamespace);
                }

                return codeGenerator.GenerateCode(xmlModelReader, outputWriter);
            }
        }
Ejemplo n.º 3
0
        internal static EdmItemCollection GetEdmItemCollectionFromArtifact(this EFArtifact artifact, out IList <EdmSchemaError> schemaErrors)
        {
            Debug.Assert(artifact != null, "Artifact is null ");

            var conceptualModel = artifact.ConceptualModel();

            EdmItemCollection edmItemCollection = null;

            schemaErrors = new List <EdmSchemaError>();

            Debug.Assert(
                conceptualModel != null && conceptualModel.XElement != null,
                "Could not find the conceptual model or its XElement in GetEdmItemCollectionFromArtifact");
            if (conceptualModel != null &&
                conceptualModel.XElement != null)
            {
                using (var xmlReader = conceptualModel.XElement.CreateReader())
                {
                    edmItemCollection = EdmItemCollection.Create(new[] { xmlReader }, null, out schemaErrors);
                }
            }

            return(edmItemCollection);
        }
Ejemplo n.º 4
0
        protected static ModelBuilderSettings SetupSettingsAndModeForDbPages(
            IServiceProvider serviceProvider,
            Project project,
            EFArtifact artifact,
            bool checkDatabaseConnection,
            ModelBuilderWizardForm.WizardMode noConnectionMode,
            ModelBuilderWizardForm.WizardMode existingConnectionMode,
            out ModelBuilderWizardForm.WizardMode startMode)
        {
            var conceptualEntityModel = artifact.ConceptualModel();
            Debug.Assert(conceptualEntityModel != null, "Null Conceptual Entity Model");
            var entityContainer = conceptualEntityModel.FirstEntityContainer as ConceptualEntityContainer;
            Debug.Assert(entityContainer != null, "Null Conceptual Entity Container");
            var entityContainerName = entityContainer.LocalName.Value;

            // set up ModelBuilderSettings for startMode=noConnectionMode
            startMode = noConnectionMode;
            var settings = new ModelBuilderSettings
            {
                VSApplicationType = VsUtils.GetApplicationType(serviceProvider, project),
                AppConfigConnectionPropertyName = entityContainerName,
                Artifact = artifact,
                UseLegacyProvider = ModelHelper.GetDesignerPropertyValueFromArtifactAsBool(
                    OptionsDesignerInfo.ElementName,
                    OptionsDesignerInfo.AttributeUseLegacyProvider,
                    OptionsDesignerInfo.UseLegacyProviderDefault,
                    artifact),
                TargetSchemaVersion = artifact.SchemaVersion,
                Project = project,
                ModelPath = artifact.Uri.LocalPath,
                ProviderManifestToken = artifact.GetProviderManifestToken()
            };

            // Get the provider manifest token from the existing SSDL.
            // We don't want to attempt to get it from provider services since this requires a connection
            // which will severely impact the performance of Model First in disconnected scenarios.

            // Change startMode and settings appropriately depending on whether there is an existing connection string and whether we can/should connect
            // to the database
            var connectionString = ConnectionManager.GetConnectionStringObject(project, entityContainerName);
            if (connectionString != null)
            {
                var ecsb = connectionString.Builder;
                var runtimeProviderName = ecsb.Provider;
                var runtimeProviderConnectionString = ecsb.ProviderConnectionString;
                var designTimeProviderConnectionString = connectionString.GetDesignTimeProviderConnectionString(project);
                var initialCatalog = String.Empty;

                if (checkDatabaseConnection)
                {
                    // This path will check to make sure that we can connect to an existing database before changing the start mode to 'existingConnection'
                    IVsDataConnection dataConnection = null;
                    try
                    {
                        var dataConnectionManager = serviceProvider.GetService(typeof(IVsDataConnectionManager)) as IVsDataConnectionManager;
                        Debug.Assert(dataConnectionManager != null, "Could not find IVsDataConnectionManager");

                        var dataProviderManager = serviceProvider.GetService(typeof(IVsDataProviderManager)) as IVsDataProviderManager;
                        Debug.Assert(dataProviderManager != null, "Could not find IVsDataProviderManager");

                        if (dataConnectionManager != null
                            && dataProviderManager != null)
                        {
                            // this will either get an existing connection or attempt to create a new one
                            dataConnection = DataConnectionUtils.GetDataConnection(
                                dataConnectionManager,
                                dataProviderManager,
                                connectionString.DesignTimeProviderInvariantName,
                                designTimeProviderConnectionString);
                            Debug.Assert(
                                dataConnection != null,
                                "Could not find the IVsDataConnection; an exception should have been thrown if this was the case");
                            if (dataConnection != null)
                            {
                                VsUtils.EnsureProvider(runtimeProviderName, settings.UseLegacyProvider, project, serviceProvider);

                                if (CanCreateAndOpenConnection(
                                    new StoreSchemaConnectionFactory(),
                                    runtimeProviderName,
                                    connectionString.DesignTimeProviderInvariantName,
                                    designTimeProviderConnectionString))
                                {
                                    startMode = existingConnectionMode;
                                    initialCatalog = DataConnectionUtils.GetInitialCatalog(dataProviderManager, dataConnection);
                                }
                            }
                        }
                    }
                    catch
                    {
                        // do nothing - we will go to WizardPageDbConfig which is
                        // what we want if the DB connection fails
                    }
                    finally
                    {
                        // Close the IVsDataConnection
                        if (dataConnection != null)
                        {
                            try
                            {
                                dataConnection.Close();
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                else
                {
                    // This path will just parse the existing connection string in order to change the start mode. This is ideal for features
                    // that do not need a database connection -- the information in the connection string is enough.
                    startMode = existingConnectionMode;
                    initialCatalog = DataConnectionUtils.GetInitialCatalog(
                        connectionString.DesignTimeProviderInvariantName, designTimeProviderConnectionString);
                }

                if (startMode == existingConnectionMode)
                {
                    // the invariant name and connection string came from app.config, so they are "runtime" invariant names and not "design-time"
                    // (Note: it is OK for InitialCatalog to be null at this stage e.g. from a provider who do not support the concept of Initial Catalog)
                    settings.SetInvariantNamesAndConnectionStrings(
                        serviceProvider,
                        project,
                        runtimeProviderName,
                        runtimeProviderConnectionString,
                        runtimeProviderConnectionString,
                        false);
                    settings.InitialCatalog = initialCatalog;
                    settings.AppConfigConnectionPropertyName = entityContainerName;
                    settings.SaveConnectionStringInAppConfig = false;

                    VsUtils.EnsureProvider(runtimeProviderName, settings.UseLegacyProvider, project, serviceProvider);
                }
            }

            return settings;
        }
        private void ProcessErrors(IList<EdmSchemaError> schemaErrors, Uri projectItemUri, EFArtifact artifact)
        {
            // since we will inevitably add *all* errors from the artifact set into the error list, we can easily
            // clear the entire error list here even though we just validate the CSDL.
            var errorList = ErrorListHelper.GetSingleDocErrorList(projectItemUri);
            if (errorList != null)
            {
                errorList.Clear();
            }

            if (artifact == null)
            {
                Debug.Fail("Where is the artifact? We should have created it at least through the temporary model manager");
                return;
            }

            // we have to remove both the RMV and SFG CSDL errors to prevent duplicates
            artifact.ArtifactSet.RemoveErrorsForArtifact(artifact, ErrorClass.Runtime_CSDL);

            // add all CSDL-based SFG errors to the artifact set
            if (schemaErrors.Count > 0)
            {
                foreach (var error in schemaErrors)
                {
                    // construct an ErrorInfo with correct line/column number and add it to the artifact set. Note that the CodeGen EdmSchemaError line
                    // refers to the line of the error inside the CSDL, so to get the line of the error in the edmx file we have to offset it by the line
                    // number where the conceptual model begins.
                    var edmxErrorLine = error.Line + artifact.ConceptualModel().GetLineNumber();
                    var efobject = artifact.FindEFObjectForLineAndColumn(edmxErrorLine, error.Column);
                    var errorInfo = new ErrorInfo(
                        GetErrorInfoSeverity(error), error.Message, efobject, error.ErrorCode, ErrorClass.Runtime_CSDL);
                    artifact.ArtifactSet.AddError(errorInfo);
                }
            }

            // get all the ErrorInfos for this artifact and add it to the error list
            var artifactSet = artifact.ArtifactSet;
            Debug.Assert(artifactSet != null, "Where is the artifact set for this artifact?");
            if (artifactSet != null)
            {
                var errors = artifactSet.GetArtifactOnlyErrors(artifact);
                if (errors.Count > 0)
                {
                    // resolve the hierarchy and item id for adding to the error list
                    var hierarchy = VSHelpers.GetVsHierarchy(ProjectItem.ContainingProject, Services.ServiceProvider);
                    var itemId = VsUtils.GetProjectItemId(hierarchy, ProjectItem);

                    Debug.Assert(hierarchy != null, "Why isn't there a hierarchy associated with this project item?");
                    Debug.Assert(itemId != VSConstants.VSITEMID_NIL, "There should be an item ID associated with this project item");

                    if (hierarchy != null
                        && itemId != VSConstants.VSITEMID_NIL)
                    {
                        ErrorListHelper.AddErrorInfosToErrorList(errors, hierarchy, itemId);
                    }
                }
            }
        }
        private string GetCodeNamespace(string defaultNamespace, EFArtifact artifact)
        {
            var ns = (defaultNamespace == null ? String.Empty : defaultNamespace.Trim());

            // This is a work-around for Astoria bug SQLBUDT 639143.  They choke when we there is no code namespace, and it is too late for them 
            // to fix this bug in the netfx, so we have the below work-around. 
            if (string.IsNullOrEmpty(ns))
            {
                if (VsUtils.GetProjectKind(Project) == VsUtils.ProjectKind.VB)
                {
                    var vbRootNamespace = VsUtils.GetProjectPropertyByName(Project, "RootNamespace") as string;
                    if (string.IsNullOrEmpty(vbRootNamespace))
                    {
                        ns = artifact.ConceptualModel().Namespace.Value;
                    }
                }
                else
                {
                    ns = artifact.ConceptualModel().Namespace.Value;
                }
            }

            return ns;
        }