Beispiel #1
0
        private AssociationType GenerateAssociationType(
            SimpleMappingContext mappingContext,
            AssociationType storeAssociationType,
            UniqueIdentifierService globallyUniqueTypeNames)
        {
            Debug.Assert(storeAssociationType.RelationshipEndMembers.Count == 2);

            var storeEndMember0 = (AssociationEndMember)storeAssociationType.RelationshipEndMembers[0];
            var storeEndMember1 = (AssociationEndMember)storeAssociationType.RelationshipEndMembers[1];

            var storeSchemaVersion = EntityFrameworkVersion.DoubleToVersion(mappingContext.StoreModel.SchemaVersion);
            var isFkAssociation    = storeSchemaVersion > EntityFrameworkVersion.Version1 &&
                                     (_generateForeignKeyProperties || RequiresReferentialConstraint(storeAssociationType));

            var uniqueEndMemberNames = new UniqueIdentifierService(StringComparer.OrdinalIgnoreCase);
            var multiplicityOverride = GetMultiplicityOverride(storeAssociationType);

            var conceptualEndMember0 = GenerateAssociationEndMember(
                mappingContext,
                storeEndMember0,
                uniqueEndMemberNames,
                multiplicityOverride);

            var conceptualEndMember1 = GenerateAssociationEndMember(
                mappingContext,
                storeEndMember1,
                uniqueEndMemberNames,
                multiplicityOverride);

            var conceptualAssociationType = AssociationType.Create(
                CreateModelName(storeAssociationType.Name, globallyUniqueTypeNames),
                _namespaceName,
                isFkAssociation,
                DataSpace.CSpace,
                conceptualEndMember0,
                conceptualEndMember1,
                CreateReferentialConstraint(mappingContext, storeAssociationType),
                null);

            CreateModelNavigationProperties(conceptualAssociationType);

            mappingContext.AddMapping(storeAssociationType, conceptualAssociationType);

            return(conceptualAssociationType);
        }
Beispiel #2
0
        // internal for testing
        internal IEnumerable <EdmFunction> GenerateFunctions(
            SimpleMappingContext mappingContext,
            EdmModel storeModel,
            UniqueIdentifierService uniqueEntityContainerNames,
            UniqueIdentifierService globallyUniqueTypeNames)
        {
            Debug.Assert(mappingContext != null, "mappingContext != null");
            Debug.Assert(storeModel != null, "storeModel != null");
            Debug.Assert(uniqueEntityContainerNames != null, "uniqueEntityContainerNames != null");
            Debug.Assert(globallyUniqueTypeNames != null, "globallyUniqueTypeNames != null");

            // TODO: Note we import only TVFs here and other store functions are
            // imported elsewhere - ideally we import all functions in one place
            // http://entityframework.codeplex.com/workitem/925
            if (EntityFrameworkVersion.DoubleToVersion(storeModel.SchemaVersion) < EntityFrameworkVersion.Version3)
            {
                yield break;
            }

            foreach (var storeFunction in storeModel.Functions)
            {
                if (storeFunction.IsComposableAttribute &&
                    !storeFunction.AggregateAttribute
                    &&
                    storeFunction.Parameters.All(p => p.Mode == ParameterMode.In))
                {
                    var functionImport = GenerateFunction(
                        mappingContext,
                        storeFunction,
                        uniqueEntityContainerNames,
                        globallyUniqueTypeNames);

                    if (functionImport != null)
                    {
                        mappingContext.AddMapping(storeFunction, functionImport);

                        yield return(functionImport);
                    }
                }
            }
        }
        public static LegacyMetadata.MetadataWorkspace ToLegacyMetadataWorkspace(this MetadataWorkspace metadataWorkspace)
        {
            Debug.Assert(metadataWorkspace != null, "metadataWorkspace != null");

            // The cloned workspace is supposed to be used only by provider. Therefore we only care about SSpace.
            // For CSpace and C-S mapping we register dummy item collections just to make the workspace checks pass.
            var legacyStoreItemCollection =
                ((StoreItemCollection)metadataWorkspace.GetItemCollection(DataSpace.SSpace)).ToLegacyStoreItemCollection();

            var version = EntityFrameworkVersion.DoubleToVersion(legacyStoreItemCollection.StoreSchemaVersion);

            var legacyEdmItemCollection = GetLegacyEdmItemCollection(version);

            var legacyWorkspace = new LegacyMetadata.MetadataWorkspace();

            legacyWorkspace.RegisterItemCollection(legacyEdmItemCollection);
            legacyWorkspace.RegisterItemCollection(legacyStoreItemCollection);

            var msl = string.Format(
                CultureInfo.InvariantCulture,
                MslTemplate,
                SchemaManager.GetMSLNamespaceName(version),
                legacyStoreItemCollection.GetItems <LegacyMetadata.EntityContainer>().Single().Name);

            using (var stringReader = new StringReader(msl))
            {
                using (var reader = XmlReader.Create(stringReader))
                {
                    var legacyMappingItemCollection =
                        new LegacyMapping.StorageMappingItemCollection(
                            legacyEdmItemCollection,
                            legacyStoreItemCollection,
                            new[] { reader });

                    legacyWorkspace.RegisterItemCollection(legacyMappingItemCollection);
                }
            }

            return(legacyWorkspace);
        }
Beispiel #4
0
        public SimpleMappingContext Build(EdmModel storeModel)
        {
            Debug.Assert(storeModel != null, "storeModel != null");

            var mappingContext = new SimpleMappingContext(storeModel, _generateForeignKeyProperties);

            var uniqueEntityContainerNames = new UniqueIdentifierService();
            var globallyUniqueTypeNames    = new UniqueIdentifierService();

            CollectForeignKeyProperties(mappingContext, storeModel);

            foreach (var storeEntitySet in storeModel.Containers.Single().EntitySets)
            {
                GenerateEntitySet(mappingContext, storeEntitySet, uniqueEntityContainerNames, globallyUniqueTypeNames);
            }

            GenerateAssociationSets(
                mappingContext,
                uniqueEntityContainerNames,
                globallyUniqueTypeNames);

            var functionImports =
                GenerateFunctions(mappingContext, storeModel, uniqueEntityContainerNames, globallyUniqueTypeNames)
                .ToArray();

            var conceptualModelContainer = EntityContainer.Create(
                _containerName,
                DataSpace.CSpace,
                mappingContext.ConceptualEntitySets()
                .Concat(mappingContext.ConceptualAssociationSets().Cast <EntitySetBase>()),
                functionImports,
                EntityFrameworkVersion.DoubleToVersion(storeModel.SchemaVersion) >= EntityFrameworkVersion.Version2
                    ? new[] { CreateAnnotationMetadataProperty("LazyLoadingEnabled", "true") }
                    : null);

            mappingContext.AddMapping(storeModel.Containers.Single(), conceptualModelContainer);

            return(mappingContext);
        }
Beispiel #5
0
 public static Version CsdlVersion(this EdmItemCollection edmItemCollection)
 {
     return(EntityFrameworkVersion.DoubleToVersion(edmItemCollection.EdmVersion));
 }
        public void ToLegacyMetadataWorkspace_creates_equivalent_legacy_MetadataWorkspace_for_all_versions()
        {
            const string ssdlTemplate =
                @"<Schema Namespace=""NorthwindEF5Model.Store"" Provider=""System.Data.SqlClient"" ProviderManifestToken=""2008"" Alias=""Self"" xmlns=""{0}"" xmlns:store=""http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"">"
                +
                @"  <EntityType Name=""Customers"">" +
                @"    <Key>" +
                @"      <PropertyRef Name=""CustomerID"" />" +
                @"    </Key>" +
                @"    <Property Name=""CustomerID"" Type=""nchar"" MaxLength=""5"" Nullable=""false"" />" +
                @"    <Property Name=""CompanyName"" Type=""nvarchar"" MaxLength=""40"" Nullable=""false"" />" +
                @"  </EntityType>" +
                @"  <EntityContainer Name=""Container"" />" +
                @"</Schema>";

            const string csdlTemplate =
                @"<Schema xmlns=""{0}"" Namespace=""dummy"">" +
                @"    <EntityContainer Name=""DummyContainer""/>" +
                @"</Schema>";

            const string mslTemplate =
                @"<Mapping Space=""C-S"" xmlns=""{0}"">" +
                @"  <EntityContainerMapping StorageEntityContainer=""Container"" CdmEntityContainer=""DummyContainer"" />" +
                @"</Mapping>";

            foreach (var version in EntityFrameworkVersion.GetAllVersions())
            {
                var storeItemCollection =
                    Utils.CreateStoreItemCollection(
                        string.Format(
                            ssdlTemplate,
                            SchemaManager.GetSSDLNamespaceName(version)));
                var edmItemCollection =
                    new EdmItemCollection(
                        new[]
                {
                    XmlReader.Create(
                        new StringReader(
                            string.Format(
                                csdlTemplate,
                                SchemaManager.GetCSDLNamespaceName(version))))
                });
                var mappingItemCollection =
                    new StorageMappingItemCollection(
                        edmItemCollection,
                        storeItemCollection,
                        new[]
                {
                    XmlReader.Create(
                        new StringReader(
                            string.Format(
                                mslTemplate,
                                SchemaManager.GetMSLNamespaceName(version))))
                });
                var workspace = new MetadataWorkspace(
                    () => edmItemCollection,
                    () => storeItemCollection,
                    () => mappingItemCollection);

                var legacyWorkspace = workspace.ToLegacyMetadataWorkspace();

                Assert.NotNull(legacyWorkspace);

                var legacyStoreItemCollection = legacyWorkspace.GetItemCollection(LegacyMetadata.DataSpace.SSpace);

                Assert.Equal(
                    storeItemCollection.GetItems <GlobalItem>().Count,
                    legacyStoreItemCollection.GetItems <LegacyMetadata.GlobalItem>().Count);

                Assert.NotNull(
                    legacyStoreItemCollection.GetItem <LegacyMetadata.EntityType>("NorthwindEF5Model.Store.Customers"));

                var legacyEdmItemCollection =
                    (LegacyMetadata.EdmItemCollection)legacyWorkspace.GetItemCollection(LegacyMetadata.DataSpace.CSpace);
                Assert.NotNull(legacyEdmItemCollection);
                Assert.Equal(version, EntityFrameworkVersion.DoubleToVersion(legacyEdmItemCollection.EdmVersion));

                Assert.NotNull(legacyWorkspace.GetItemCollection(LegacyMetadata.DataSpace.CSSpace));
            }
        }