internal CreateEntityContainerMappingCommand(EFArtifact artifact)
        {
            Debug.Assert(artifact != null, "artifact should not be null");
            Debug.Assert(
                artifact.ConceptualModel().EntityContainerCount == 1,
                "conceptual model EntityContainer count (" + artifact.ConceptualModel().EntityContainerCount + ") should be 1");
            Debug.Assert(
                artifact.StorageModel().EntityContainerCount == 1,
                "storage model EntityContainer count (" + artifact.StorageModel().EntityContainerCount + ") should be 1");
            Debug.Assert(
                artifact.MappingModel().FirstEntityContainerMapping == null, "mapping model FirstEntityContainer should not be null");

            _artifact = artifact;
        }
        internal CreateEntityContainerMappingCommand(EFArtifact artifact)
        {
            Debug.Assert(artifact != null, "artifact should not be null");
            Debug.Assert(
                artifact.ConceptualModel().EntityContainerCount == 1,
                "conceptual model EntityContainer count (" + artifact.ConceptualModel().EntityContainerCount + ") should be 1");
            Debug.Assert(
                artifact.StorageModel().EntityContainerCount == 1,
                "storage model EntityContainer count (" + artifact.StorageModel().EntityContainerCount + ") should be 1");
            Debug.Assert(
                artifact.MappingModel().FirstEntityContainerMapping == null, "mapping model FirstEntityContainer should not be null");

            _artifact = artifact;
        }
示例#3
0
        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);
                    }
                }
            }
        }
        public static FunctionImport GetFreshFunctionImport(this EFArtifact artifact, string functionImportName)
        {
            Debug.Assert(artifact != null, "artifact != null");
            Debug.Assert(!string.IsNullOrWhiteSpace(functionImportName), "!string.IsNullOrWhiteSpace(functionImportName)");

            return((FunctionImport)artifact.ConceptualModel().EntityContainers().Single().GetFirstNamedChildByLocalName(functionImportName));
        }
        public static ConceptualEntityContainer GetFreshConceptualEntityContainer(this EFArtifact artifact, string entityContainerName)
        {
            Debug.Assert(artifact != null, "artifact != null");
            Debug.Assert(!string.IsNullOrWhiteSpace(entityContainerName), "!string.IsNullOrWhiteSpace(entityContainerName)");

            return
                ((ConceptualEntityContainer)
                 artifact.ConceptualModel().EntityContainers().SingleOrDefault(e => e.Name.Value == entityContainerName));
        }
        public static T GetConceptualChildByName <T>(this EFArtifact artifact, string childName)
            where T : EFNameableItem
        {
            Debug.Assert(artifact != null, "artifact != null");

            var conceptualModel = artifact.ConceptualModel();

            return((T)conceptualModel.GetFirstNamedChildByLocalName(childName));
        }
        private void PropagateAllStoragePropertyFacets(EFArtifact artifact)
        {
            var sModel = artifact.StorageModel();

            if (null == sModel)
            {
                Debug.Fail("null StorageEntityModel");
                return;
            }

            var cModel = artifact.ConceptualModel();

            if (null == cModel)
            {
                Debug.Fail("null ConceptualEntityModel");
                return;
            }

            // loop over every S-side Property
            foreach (var sSideEntityType in sModel.EntityTypes())
            {
                foreach (var sSideProperty in sSideEntityType.Properties())
                {
                    // add every mapped C-side Property whose parent is a C-side EntityType (as opposed to a ComplexType)
                    var cSideProperties = new HashSet <Property>();
                    foreach (var scalarProp in sSideProperty.GetAntiDependenciesOfType <ScalarProperty>())
                    {
                        // only count mappings through EntitySetMapping and EntityTypeMapping (MappingFragment only
                        // appears as a child in these kinds of mappings). Mappings through e.g.
                        // AssociationSetMapping do not identify the C-side properties to be updated.
                        if (scalarProp.GetParentOfType(typeof(MappingFragment)) != null)
                        {
                            var cSideProperty = scalarProp.Name.Target;
                            if (null != cSideProperty &&
                                null != cSideProperty.GetParentOfType(typeof(EntityType)) &&
                                false == cSideProperties.Contains(cSideProperty))
                            {
                                cSideProperties.Add(cSideProperty);
                            }
                        }
                    }

                    // propagate the facets for this S-side Property to all the C-side Properties that are mapped to it
                    foreach (var mappedCSideProperty in cSideProperties)
                    {
                        // for each mapped C-side Property, loop over all the facet-synchronizers invoking them
                        foreach (var facetSynchronizer in _facetSynchronizers)
                        {
                            facetSynchronizer.Invoke(sSideProperty, mappedCSideProperty);
                        }
                    }
                }
            }
        }
示例#8
0
        private void UnloadLayer(IEntityDesignerLayer layer)
        {
            if (_artifact != null &&
                _artifact.ConceptualModel() != null)
            {
                layer.OnBeforeLayerUnloaded(_artifact.ConceptualModel().XObject);
            }

            foreach (var command in GetCommands(layer))
            {
                RemoveCommand(command);
            }

            layer.ChangeEntityDesignerSelection -= layer_EntityDesignerSelectionChanged;

            if (_selectionContainer != null)
            {
                _selectionContainer.Dispose();
                _selectionContainer = null;
            }
        }
示例#9
0
        internal static AssociationSummary ConstructAssociationSummary(EFArtifact artifact)
        {
            var ecm = artifact.MappingModel().FirstEntityContainerMapping;

            var summary = new AssociationSummary();

            if (!EdmFeatureManager.GetForeignKeysInModelFeatureState(artifact.SchemaVersion).IsEnabled())
            {
                if (ecm != null)
                {
                    // Foreign keys in the model are not supported for this EDMX version.
                    foreach (var asm in ecm.AssociationSetMappings())
                    {
                        var cSideAssociation = asm.TypeName.Target;

                        if (null != cSideAssociation)
                        {
                            var assocId = AssociationIdentityForAssociationSetMapping.CreateAssociationIdentity(asm);
                            if (null != assocId)
                            {
                                summary.Add(cSideAssociation, assocId);
                            }
                        }
                    }
                }
            }
            else
            {
                // Foreign keys in the model are supported for this EDMX version.
                foreach (var a in artifact.ConceptualModel().Associations())
                {
                    AssociationIdentity assocId = null;
                    if (a.IsManyToMany == false &&
                        a.ReferentialConstraint != null)
                    {
                        assocId = AssociationIdentityForReferentialConstraint.CreateAssociationIdentity(a.ReferentialConstraint);
                    }
                    else
                    {
                        var asm = ModelHelper.FindAssociationSetMappingForConceptualAssociation(a);
                        if (asm != null)
                        {
                            assocId = AssociationIdentityForAssociationSetMapping.CreateAssociationIdentity(asm);
                        }
                    }
                    if (null != assocId)
                    {
                        summary.Add(a, assocId);
                    }
                }
            }
            return(summary);
        }
        internal ExistingModelSummary(EFArtifact artifact)
        {
            _artifact = artifact;
            if (null == artifact)
            {
                Debug.Fail("Null artifact");
            }
            else
            {
                if (null != artifact.MappingModel()
                    && null != artifact.MappingModel().FirstEntityContainerMapping)
                {
                    RecordEntityTypeIdentities(
                        artifact.MappingModel().FirstEntityContainerMapping);

                    // build the association summary.
                    _associationSummary = AssociationSummary.ConstructAssociationSummary(artifact);
                }

                if (null != artifact.ConceptualModel())
                {
                    RecordInheritanceAndEntityTypeMappings(artifact.ConceptualModel());
                }

                if (null != artifact.StorageModel())
                {
                    RecordFunctions(artifact.StorageModel());

                    if (null != artifact.StorageModel().FirstEntityContainer)
                    {
                        var sec = artifact.StorageModel().FirstEntityContainer as StorageEntityContainer;
                        if (sec != null)
                        {
                            RecordStorageEntitySetsAndProperties(sec);
                        }
                    }
                }
            }
        }
示例#11
0
        internal ExistingModelSummary(EFArtifact artifact)
        {
            _artifact = artifact;
            if (null == artifact)
            {
                Debug.Fail("Null artifact");
            }
            else
            {
                if (null != artifact.MappingModel() &&
                    null != artifact.MappingModel().FirstEntityContainerMapping)
                {
                    RecordEntityTypeIdentities(
                        artifact.MappingModel().FirstEntityContainerMapping);

                    // build the association summary.
                    _associationSummary = AssociationSummary.ConstructAssociationSummary(artifact);
                }

                if (null != artifact.ConceptualModel())
                {
                    RecordInheritanceAndEntityTypeMappings(artifact.ConceptualModel());
                }

                if (null != artifact.StorageModel())
                {
                    RecordFunctions(artifact.StorageModel());

                    if (null != artifact.StorageModel().FirstEntityContainer)
                    {
                        var sec = artifact.StorageModel().FirstEntityContainer as StorageEntityContainer;
                        if (sec != null)
                        {
                            RecordStorageEntitySetsAndProperties(sec);
                        }
                    }
                }
            }
        }
示例#12
0
        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);
        }
        protected override void InvokeInternal(CommandProcessorContext cpc)
        {
            if (_artifact == null)
            {
                throw new InvalidOperationException("InvokeInternal is called when _artifact is null");
            }

            var ecm = new EntityContainerMapping(_artifact.MappingModel(), null);

            ecm.CdmEntityContainer.SetRefName(_artifact.ConceptualModel().FirstEntityContainer);
            ecm.StorageEntityContainer.SetRefName(_artifact.StorageModel().FirstEntityContainer);
            _artifact.MappingModel().AddEntityContainerMapping(ecm);

            XmlModelHelper.NormalizeAndResolve(ecm);

            _created = ecm;
        }
        internal static EdmItemCollection GetEdmItemCollectionFromArtifact(EFArtifact artifact, out IList <EdmSchemaError> schemaErrors)
        {
            Debug.Assert(artifact != null, "Artifact is null ");
            EdmItemCollection edmItemCollection = null;

            schemaErrors = new List <EdmSchemaError>();

            var conceptualModel = artifact.ConceptualModel();

            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);
        }
示例#15
0
        internal static void SetupSettingsAndModeForDbPages(
            IServiceProvider sp,
            Project project,
            EFArtifact artifact,
            bool checkDatabaseConnection,
            ModelBuilderWizardForm.WizardMode noConnectionMode,
            ModelBuilderWizardForm.WizardMode existingConnectionMode,
            out ModelBuilderWizardForm.WizardMode startMode,
            out ModelBuilderSettings settings)
        {
            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;
            settings  = new ModelBuilderSettings();
            var appType = VsUtils.GetApplicationType(sp, project);

            settings.VSApplicationType = appType;
            settings.AppConfigConnectionPropertyName = entityContainerName;
            settings.Artifact          = artifact;
            settings.UseLegacyProvider = ModelHelper.GetDesignerPropertyValueFromArtifactAsBool(
                OptionsDesignerInfo.ElementName,
                OptionsDesignerInfo.AttributeUseLegacyProvider,
                OptionsDesignerInfo.UseLegacyProviderDefault,
                artifact);
            settings.TargetSchemaVersion = artifact.SchemaVersion;
            settings.Project             = project;
            settings.ModelPath           = artifact.Uri.LocalPath;

            // 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.
            settings.ProviderManifestToken = DatabaseGenerationEngine.GetProviderManifestTokenDisconnected(artifact);

            // 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 = sp.GetService(typeof(IVsDataConnectionManager)) as IVsDataConnectionManager;
                        Debug.Assert(dataConnectionManager != null, "Could not find IVsDataConnectionManager");

                        var dataProviderManager = sp.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, sp);

                                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(
                        project,
                        runtimeProviderName,
                        runtimeProviderConnectionString,
                        runtimeProviderConnectionString,
                        false);
                    settings.InitialCatalog = initialCatalog;
                    settings.AppConfigConnectionPropertyName = entityContainerName;
                    settings.SaveConnectionStringInAppConfig = false;

                    VsUtils.EnsureProvider(runtimeProviderName, settings.UseLegacyProvider, project, sp);
                }
            }
        }
        internal static AssociationSummary ConstructAssociationSummary(EFArtifact artifact)
        {
            var ecm = artifact.MappingModel().FirstEntityContainerMapping;

            var summary = new AssociationSummary();

            if (!EdmFeatureManager.GetForeignKeysInModelFeatureState(artifact.SchemaVersion).IsEnabled())
            {
                if (ecm != null)
                {
                    // Foreign keys in the model are not supported for this EDMX version.
                    foreach (var asm in ecm.AssociationSetMappings())
                    {
                        var cSideAssociation = asm.TypeName.Target;

                        if (null != cSideAssociation)
                        {
                            var assocId = AssociationIdentityForAssociationSetMapping.CreateAssociationIdentity(asm);
                            if (null != assocId)
                            {
                                summary.Add(cSideAssociation, assocId);
                            }
                        }
                    }
                }
            }
            else
            {
                // Foreign keys in the model are supported for this EDMX version.
                foreach (var a in artifact.ConceptualModel().Associations())
                {
                    AssociationIdentity assocId = null;
                    if (a.IsManyToMany == false
                        && a.ReferentialConstraint != null)
                    {
                        assocId = AssociationIdentityForReferentialConstraint.CreateAssociationIdentity(a.ReferentialConstraint);
                    }
                    else
                    {
                        var asm = ModelHelper.FindAssociationSetMappingForConceptualAssociation(a);
                        if (asm != null)
                        {
                            assocId = AssociationIdentityForAssociationSetMapping.CreateAssociationIdentity(asm);
                        }
                    }
                    if (null != assocId)
                    {
                        summary.Add(a, assocId);
                    }
                }
            }
            return summary;
        }