Beispiel #1
0
        public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
        {
            var role = builder.AddEnumType(typeof(Role));

            role.Name = typeof(Role).GetAttribute <DescriptionAttribute>()
                        ?.Description
                        ?? nameof(Role);
        }
Beispiel #2
0
        public static IEdmModel GetEdmModel()
        {
            //ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var builder = new ODataModelBuilder {
                Namespace = typeof(Person).Namespace, ContainerName = "DefaultContainer"
            };

            ComplexTypeConfiguration <Range> rangeType = builder.ComplexType <Range>();

            //rangeType.Select();
            rangeType.Property(c => c.minValue);
            rangeType.Property(c => c.maxValue);

            var personConfig = builder.AddEntityType(typeof(Person));

            personConfig.HasKey(typeof(Person).GetProperty("ID"));
            personConfig.AddProperty(typeof(Person).GetProperty("Name"));
            personConfig.AddNavigationProperty(typeof(Person).GetProperty("Car"), EdmMultiplicity.ZeroOrOne);
            //personConfig.AddComplexProperty(typeof(Person).GetProperty("minValue"));
            //personConfig.AddComplexProperty(typeof(Person).GetProperty("maxValue"));
            personConfig.AddComplexProperty(typeof(Person).GetProperty("Ranges"));

            var carConfig = builder.AddEntityType(typeof(Car));

            carConfig.HasKey(typeof(Car).GetProperty("ID"));
            carConfig.AddProperty(typeof(Car).GetProperty("AmountMade"));
            carConfig.AddProperty(typeof(Car).GetProperty("APK"));
            carConfig.AddProperty(typeof(Car).GetProperty("Name"));
            carConfig.AddProperty(typeof(Car).GetProperty("TimeWhenAddedToDatabase"));
            carConfig.AddEnumProperty(typeof(Car).GetProperty("Brand"));
            carConfig.AddNavigationProperty(typeof(Car).GetProperty("People"), EdmMultiplicity.Many);

            var _Brands = builder.AddEnumType(typeof(_Brands));

            _Brands.AddMember(Models._Brands.Audi);
            _Brands.AddMember(Models._Brands.BMW);
            _Brands.AddMember(Models._Brands.Ferrari);
            _Brands.AddMember(Models._Brands.Ford);
            _Brands.AddMember(Models._Brands.Honda);
            _Brands.AddMember(Models._Brands.Mercedes);
            _Brands.AddMember(Models._Brands.Mini);
            _Brands.AddMember(Models._Brands.Nissan);
            _Brands.AddMember(Models._Brands.Porsche);
            _Brands.AddMember(Models._Brands.Tesla);
            _Brands.AddMember(Models._Brands.Toyota);
            _Brands.AddMember(Models._Brands.Volkswagen);

            builder.AddEntitySet("people", personConfig);
            builder.AddEntitySet("cars", carConfig);

            var edmModel = builder.GetEdmModel();

            //return builder.GetEdmModel();
            return(edmModel);
        }
        public void CtorODataQueryContext_TakingClrType_WithEnumTypes(Type type)
        {
            // Arrange
            Type enumType = Nullable.GetUnderlyingType(type) ?? type;

            ODataModelBuilder builder = new ODataModelBuilder();

            builder.AddEnumType(enumType);
            IEdmModel model = builder.GetEdmModel();

            // Act
            ODataQueryContext context = new ODataQueryContext(model, type);

            // Assert
            Assert.True(context.ElementClrType == type);
        }
Beispiel #4
0
        public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
        {
            var p = typeof(Infrastructure.Enums.Role).GetProperties();

            _context.GetDbSets().ForEach(entityType =>
            {
                if (entityType.IsEnum)
                {
                    var name = entityType.Name;//entityType.GetAttribute<DescriptionAttribute>().Description ?? entityType.Name;
                    if (builder.EnumTypes.FirstOrDefault(k => k.Name == name) == null)
                    {
                        builder.AddEnumType(entityType);
                    }
                }
                else if (builder.EntitySets.FirstOrDefault(k => k.Name == entityType.Name) == null)
                {
                    var primaryKeys = _context.ModelBuilder.Value.Entity(entityType).Metadata.FindPrimaryKey()
                                      ?.Properties
                                      ?.Select(m => m.PropertyInfo)
                                      ?.ToList()
                                      ?? entityType.GetProperties().Take(1).ToList()
                                      ?? throw new ArgumentNullException();

                    if (primaryKeys != null)
                    {
                        var entity = builder.AddEntitySet(entityType.Name, builder.AddEntityType(entityType)).EntityType;
                        primaryKeys.ForEach(primaryKey => entity.HasKey(primaryKey));
                    }

                    /*else if (builder.StructuralTypes.FirstOrDefault(k => k.Name == entityType.Name) == null)
                     * {
                     *  builder.AddComplexType(entityType);
                     * }*/
                }
            });
        }
Beispiel #5
0
        public void PassNotEnumTypeToODataModelBuilderAddEnumTypeShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            Type type = typeof(Int32);

            // Act & Assert
            Assert.ThrowsArgument(
                () => builder.AddEnumType(type),
                "type",
                "The type 'System.Int32' cannot be configured as an enum type.");
        }
Beispiel #6
0
        public void PassNullToODataModelBuilderAddEnumTypeShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => builder.AddEnumType(null),
                "type");
        }