Ejemplo n.º 1
0
 static TypeExtensions()
 {
     foreach (var primitiveType in PrimitiveType.GetEdmPrimitiveTypes())
     {
         if (!_primitiveTypesMap.ContainsKey(primitiveType.ClrEquivalentType))
         {
             _primitiveTypesMap.Add(primitiveType.ClrEquivalentType, primitiveType);
         }
     }
 }
Ejemplo n.º 2
0
 static TypeExtensions()
 {
     foreach (PrimitiveType edmPrimitiveType in PrimitiveType.GetEdmPrimitiveTypes())
     {
         if (!TypeExtensions._primitiveTypesMap.ContainsKey(edmPrimitiveType.ClrEquivalentType))
         {
             TypeExtensions._primitiveTypesMap.Add(edmPrimitiveType.ClrEquivalentType, edmPrimitiveType);
         }
     }
 }
Ejemplo n.º 3
0
        private FunctionParameter CreateReturnParameter(MethodInfo method, DbModel model)
        {
            Type              parameterType     = method.ReturnType;
            string            parameterName     = "return";
            PrimitiveType     primitiveType     = PrimitiveType.GetEdmPrimitiveTypes().FirstOrDefault(t => t.ClrEquivalentType == parameterType);
            EdmType           edmType           = model.ProviderManifest.GetStoreType(TypeUsage.CreateDefaultTypeUsage(primitiveType)).EdmType;
            ParameterMode     parameterMode     = ParameterMode.ReturnValue;
            FunctionParameter functionParameter = FunctionParameter.Create(parameterName, edmType, parameterMode);

            return(functionParameter);
        }
Ejemplo n.º 4
0
        public void GetAllPrimitiveTypes_returns_all_primitive_types_for_version()
        {
            foreach (var schemaVersion in EntityFrameworkVersion.GetAllVersions())
            {
                // remove geo spatial types for schema versions V1 and V2
                var expectedTypes =
                    PrimitiveType
                    .GetEdmPrimitiveTypes()
                    .Where(t => schemaVersion == EntityFrameworkVersion.Version3 || !IsGeoSpatialType(t))
                    .Select(t => t.Name);

                Assert.Equal(expectedTypes, ModelHelper.AllPrimitiveTypes(schemaVersion));
            }
        }
        private static PrimitiveType GetModelPrimitiveType(DbModel model, Type clrType, MethodInfo methodInfo, bool errorIfNotFound = true)
        {
            // Parameter and return parameter can be Nullable<T>.
            // Return parameter can be IQueryable<T>, ObjectResult<T>.
            if (clrType.IsGenericType)
            {
                var genericTypeDefinition = clrType.GetGenericTypeDefinition();
                if (genericTypeDefinition == typeof(Nullable <>) ||
                    genericTypeDefinition == typeof(IQueryable <>) ||
                    genericTypeDefinition == typeof(ObjectResult <>))
                {
                    clrType = clrType.GetGenericArguments().Single();                     // Gets T from Nullable<T>.
                }
            }

            if (clrType.IsEnum)
            {
                var modelEnumType = model
                                    .ConceptualModel
                                    .EnumTypes
                                    .FirstOrDefault(enumType => enumType.FullName.Equals(clrType.FullName, StringComparison.Ordinal));
                if (modelEnumType == null)
                {
                    throw new NotSupportedException(
                              string.Format("Enum type {1} in method {0} is not supported in conceptual model.", methodInfo.Name, clrType.FullName));
                }

                return(modelEnumType.UnderlyingType);
            }

            // clrType is not enum.
            PrimitiveType modelPrimitiveType = PrimitiveType
                                               .GetEdmPrimitiveTypes()
                                               .FirstOrDefault(primitiveType => primitiveType.ClrEquivalentType == clrType);

            if (modelPrimitiveType == null)
            {
                if (!errorIfNotFound)
                {
                    return(null);
                }

                throw new NotSupportedException(
                          string.Format("Type {1} in method {0} is not supported in conceptual model.", methodInfo.Name, clrType.FullName));
            }

            return(modelPrimitiveType);
        }
Ejemplo n.º 6
0
        public List <EdmMember> GetEdmMembers(DbModel model)
        {
            List <PropertyInfo> properties = this.mType.GetProperties().ToList();
            List <EdmMember>    members    = new List <EdmMember>();

            foreach (PropertyInfo property in properties)
            {
                PrimitiveType primitiveType = PrimitiveType.GetEdmPrimitiveTypes().FirstOrDefault(t => t.ClrEquivalentType == property.PropertyType);
                EdmType       edmType       = model.ProviderManifest.GetStoreType(TypeUsage.CreateDefaultTypeUsage(primitiveType)).EdmType;
                TypeUsage     typeUsage     = TypeUsage.CreateDefaultTypeUsage(edmType);
                EdmProperty   edmProperty   = EdmProperty.Create(property.Name, typeUsage);
                edmProperty.Nullable = false;
                members.Add(edmProperty);
            }
            return(members);
        }
Ejemplo n.º 7
0
        private List <FunctionParameter> CreateFunctionParameters(MethodInfo method, DbModel model)
        {
            ParameterInfo[]          parameters         = method.GetParameters();
            List <FunctionParameter> functionParameters = new List <FunctionParameter>();

            foreach (var parameter in parameters)
            {
                string            parameterName     = parameter.Name;
                PrimitiveType     primitiveType     = PrimitiveType.GetEdmPrimitiveTypes().FirstOrDefault(t => t.ClrEquivalentType == parameter.ParameterType);
                EdmType           edmType           = model.ProviderManifest.GetStoreType(TypeUsage.CreateDefaultTypeUsage(primitiveType)).EdmType;
                ParameterMode     parameterMode     = !parameter.IsIn ? ParameterMode.In : ParameterMode.Out;
                FunctionParameter functionParameter = FunctionParameter.Create(parameterName, edmType, parameterMode);
                functionParameters.Add(functionParameter);
            }
            return(functionParameters);
        }
Ejemplo n.º 8
0
        private static SimpleType GetSimpleEdmType(DbModel model, Type type)
        {
            SimpleType edmType = null;

            type = Nullable.GetUnderlyingType(type) ?? type;
            if (type.IsEnum)
            {
                edmType = model.ConceptualModel.EnumTypes.FirstOrDefault(t => t.FullName == type.FullName);
                if (edmType != null)
                {
                    return(edmType);
                }
                type = Enum.GetUnderlyingType(type);
            }
            edmType = PrimitiveType.GetEdmPrimitiveTypes().FirstOrDefault(t => t.ClrEquivalentType == type);
            return(edmType);
        }
        static EdmType MapTypeToEdmType(Type type)
        {
            var fromClrType = PrimitiveType
                              .GetEdmPrimitiveTypes()
                              .FirstOrDefault(t => t.ClrEquivalentType == type);

            if (fromClrType != null)
            {
                return(fromClrType);
            }

            if (type.IsEnum)
            {
                return(MapTypeToEdmType(Enum.GetUnderlyingType(type)));
            }

            throw new NotSupportedException($"Unsupported type for mapping to EdmType: {type.FullName}");
        }
Ejemplo n.º 10
0
        private static PrimitiveType GetModelPrimitiveType(this DbModel model, Type clrType, MethodInfo methodInfo)
        {
            // Parameter and return parameter can be Nullable<T>.
            // Return parameter can be IQueryable<T>, ObjectResult<T>.
            if (clrType.IsGenericType)
            {
                Type genericTypeDefinition = clrType.GetGenericTypeDefinition();
                if (genericTypeDefinition == typeof(Nullable <>) ||
                    genericTypeDefinition == typeof(IQueryable <>) ||
                    genericTypeDefinition == typeof(ObjectResult <>))
                {
                    clrType = clrType.GetGenericArguments().Single(); // Gets T from Nullable<T>.
                }
            }

            if (clrType.IsEnum)
            {
                EnumType modelEnumType = model
                                         .ConceptualModel
                                         .EnumTypes
                                         .FirstOrDefault(enumType => enumType.FullName.EqualsOrdinal(clrType.FullName));
                if (modelEnumType == null)
                {
                    throw new NotSupportedException(
                              $"Enum type {nameof(clrType.FullName)} in method {methodInfo.Name} is not supported in conceptual model.");
                }

                return(modelEnumType.UnderlyingType);
            }

            // clrType is not enum.
            PrimitiveType modelPrimitiveType = PrimitiveType
                                               .GetEdmPrimitiveTypes()
                                               .FirstOrDefault(primitiveType => primitiveType.ClrEquivalentType == clrType);

            if (modelPrimitiveType == null)
            {
                throw new NotSupportedException(
                          $"Type {nameof(clrType.FullName)} in method {methodInfo.Name} is not supported in conceptual model.");
            }

            return(modelPrimitiveType);
        }
Ejemplo n.º 11
0
 private static EdmType GetEdmPrimitiveTypeForClrType(Type clrType)
 {
     return(PrimitiveType
            .GetEdmPrimitiveTypes()
            .FirstOrDefault(t => t.ClrEquivalentType == clrType));
 }
Ejemplo n.º 12
0
        private static DbCompiledModel CreateModel(NpgsqlConnection connection)
        {
            var dbModelBuilder = new DbModelBuilder(DbModelBuilderVersion.Latest);

            // Import Sets
            dbModelBuilder.Entity <Blog>();
            dbModelBuilder.Entity <Post>();
            dbModelBuilder.Entity <NoColumnsEntity>();
            dbModelBuilder.Entity <User>();
            dbModelBuilder.Entity <Editor>();
            dbModelBuilder.Entity <Administrator>();

            // Import function
            var dbModel = dbModelBuilder.Build(connection);
            var edmType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32);

            var addFunc = EdmFunction.Create(
                "ClrStoredAddFunction",
                "BloggingContext",
                DataSpace.SSpace,
                new EdmFunctionPayload
            {
                ParameterTypeSemantics = ParameterTypeSemantics.AllowImplicitConversion,
                Schema                 = "dbo",
                IsComposable           = true,
                IsNiladic              = false,
                IsBuiltIn              = false,
                IsAggregate            = false,
                IsFromProviderManifest = true,
                StoreFunctionName      = "StoredAddFunction",
                ReturnParameters       = new[]
                {
                    FunctionParameter.Create("ReturnType", edmType, ParameterMode.ReturnValue)
                },
                Parameters = new[]
                {
                    FunctionParameter.Create("Value1", edmType, ParameterMode.In),
                    FunctionParameter.Create("Value2", edmType, ParameterMode.In)
                }
            },
                null);

            dbModel.StoreModel.AddItem(addFunc);

            var echoFunc = EdmFunction.Create(
                "StoredEchoFunction",
                "BloggingContext",
                DataSpace.SSpace,
                new EdmFunctionPayload
            {
                ParameterTypeSemantics = ParameterTypeSemantics.AllowImplicitConversion,
                Schema                 = "dbo",
                IsComposable           = true,
                IsNiladic              = false,
                IsBuiltIn              = false,
                IsAggregate            = false,
                IsFromProviderManifest = true,
                StoreFunctionName      = null, // intentional
                ReturnParameters       = new[]
                {
                    FunctionParameter.Create("ReturnType", edmType, ParameterMode.ReturnValue)
                },
                Parameters = new[]
                {
                    FunctionParameter.Create("Value1", edmType, ParameterMode.In)
                }
            },
                null);

            dbModel.StoreModel.AddItem(echoFunc);

            var stringStoreType    = dbModel.ProviderManifest.GetStoreTypes().First(x => x.ClrEquivalentType == typeof(string));
            var modelBlogStoreType = dbModel.StoreModel.EntityTypes.First(x => x.Name == typeof(Blog).Name);
            var rowType            = RowType.Create(
                modelBlogStoreType.Properties.Select(x =>
            {
                var clone                   = EdmProperty.Create(x.Name, x.TypeUsage);
                clone.CollectionKind        = x.CollectionKind;
                clone.ConcurrencyMode       = x.ConcurrencyMode;
                clone.IsFixedLength         = x.IsFixedLength;
                clone.IsMaxLength           = x.IsMaxLength;
                clone.IsUnicode             = x.IsUnicode;
                clone.MaxLength             = x.MaxLength;
                clone.Precision             = x.Precision;
                clone.Scale                 = x.Scale;
                clone.StoreGeneratedPattern = x.StoreGeneratedPattern;
                clone.SetMetadataProperties(x
                                            .MetadataProperties
                                            .Where(metadataProerty => !clone
                                                   .MetadataProperties
                                                   .Any(cloneMetadataProperty => cloneMetadataProperty.Name.Equals(metadataProerty.Name))));
                return(clone);
            }),
                null);

            var getBlogsFunc = EdmFunction.Create(
                "StoredGetBlogsFunction",
                "BloggingContext",
                DataSpace.SSpace,
                new EdmFunctionPayload
            {
                ParameterTypeSemantics = ParameterTypeSemantics.AllowImplicitConversion,
                Schema            = "dbo",
                IsComposable      = true,
                IsNiladic         = false,
                IsBuiltIn         = false,
                IsAggregate       = false,
                StoreFunctionName = "GetBlogsByName",
                ReturnParameters  = new[]
                {
                    FunctionParameter.Create("ReturnType1", rowType.GetCollectionType(), ParameterMode.ReturnValue)
                },
                Parameters = new[]
                {
                    FunctionParameter.Create("Name", stringStoreType, ParameterMode.In)
                }
            },
                null);

            dbModel.StoreModel.AddItem(getBlogsFunc);

            var         stringPrimitiveType     = PrimitiveType.GetEdmPrimitiveTypes().First(x => x.ClrEquivalentType == typeof(string));
            var         modelBlogConceptualType = dbModel.ConceptualModel.EntityTypes.First(x => x.Name == typeof(Blog).Name);
            EdmFunction getBlogsFuncModel       = EdmFunction.Create(
                "GetBlogsByName",
                dbModel.ConceptualModel.Container.Name,
                DataSpace.CSpace,
                new EdmFunctionPayload
            {
                IsFunctionImport = true,
                IsComposable     = true,
                Parameters       = new[]
                {
                    FunctionParameter.Create("Name", stringPrimitiveType, ParameterMode.In)
                },
                ReturnParameters = new[]
                {
                    FunctionParameter.Create("ReturnType1", modelBlogConceptualType.GetCollectionType(), ParameterMode.ReturnValue)
                },
                EntitySets = new[]
                {
                    dbModel.ConceptualModel.Container.EntitySets.First(x => x.ElementType == modelBlogConceptualType)
                }
            },
                null);

            dbModel.ConceptualModel.Container.AddFunctionImport(getBlogsFuncModel);

            dbModel.ConceptualToStoreMapping.AddFunctionImportMapping(new FunctionImportMappingComposable(
                                                                          getBlogsFuncModel,
                                                                          getBlogsFunc,
                                                                          new FunctionImportResultMapping(),
                                                                          dbModel.ConceptualToStoreMapping));

            var compiledModel = dbModel.Compile();

            return(compiledModel);
        }