当前位置: 首页>>代码示例>>C#>>正文


C# ODataConventionModelBuilder.AddEntitySet方法代码示例

本文整理汇总了C#中ODataConventionModelBuilder.AddEntitySet方法的典型用法代码示例。如果您正苦于以下问题:C# ODataConventionModelBuilder.AddEntitySet方法的具体用法?C# ODataConventionModelBuilder.AddEntitySet怎么用?C# ODataConventionModelBuilder.AddEntitySet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ODataConventionModelBuilder的用法示例。


在下文中一共展示了ODataConventionModelBuilder.AddEntitySet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetModel

        public override IEdmModel GetModel(Type elementClrType, HttpRequestMessage request,
            HttpActionDescriptor actionDescriptor)
        {
            // Get model for the request
            IEdmModel model = request.ODataProperties().Model;

            if (model == null)
            {
                // user has not configured anything or has registered a model without the element type
                // let's create one just for this type and cache it in the action descriptor
                model = actionDescriptor.Properties.GetOrAdd("System.Web.OData.Model+" + elementClrType.FullName, _ =>
                {
                    ODataConventionModelBuilder builder =
                        new ODataConventionModelBuilder(actionDescriptor.Configuration, isQueryCompositionMode: true);
                    builder.EnableLowerCamelCase();
                    EntityTypeConfiguration entityTypeConfiguration = builder.AddEntityType(elementClrType);
                    builder.AddEntitySet(elementClrType.Name, entityTypeConfiguration);
                    IEdmModel edmModel = builder.GetEdmModel();
                    Contract.Assert(edmModel != null);
                    return edmModel;
                }) as IEdmModel;
            }

            Contract.Assert(model != null);
            return model;
        }
开发者ID:tanjinfu,项目名称:WebApiODataSamples,代码行数:26,代码来源:MyEnableQueryAttribute.cs

示例2: BuildEdmModel

        public static IEdmModel BuildEdmModel(Type ApiContextType)
        {
            ODataModelBuilder builder = new ODataConventionModelBuilder();
            builder.Namespace = ApiContextType.Namespace;

            var publicProperties = ApiContextType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (var property in publicProperties)
            {
                var entityClrType = TypeHelper.GetImplementedIEnumerableType(property.PropertyType);
                EntityTypeConfiguration entity = builder.AddEntityType(entityClrType);
                builder.AddEntitySet(property.Name, entity);
            }

            return builder.GetEdmModel();
        }
开发者ID:genusP,项目名称:WebApi,代码行数:15,代码来源:DefaultODataModelProvider.cs

示例3: CreateImplicitModel

        private static IEdmModel CreateImplicitModel(HttpConfiguration configuration, Type elementClrType)
        {
            ODataConventionModelBuilder builder =
                new ODataConventionModelBuilder(configuration, isQueryCompositionMode: true);

            // Add the type to the model as an entity and add an entity set with the same name.
            EntityTypeConfiguration entityTypeConfiguration = builder.AddEntityType(elementClrType);
            builder.AddEntitySet(elementClrType.Name, entityTypeConfiguration);

            // Build the model and add the AuthorizedRolesAnnotation.
            IEdmModel edmModel = builder.GetEdmModel();
            Contract.Assert(edmModel != null);

            edmModel.AddAuthorizedRolesAnnotations();
            return edmModel;
        }
开发者ID:nickgoodrow,项目名称:ODataSamples,代码行数:16,代码来源:AuthorizationEnableQueryAttribute.cs

示例4: BuildEdmModel

        public static IEdmModel BuildEdmModel(Type apiContextType, AssembliesResolver assembliesResolver, Action<ODataConventionModelBuilder> after = null)
        {
            var builder = new ODataConventionModelBuilder(assembliesResolver)
            {
                Namespace = apiContextType.Namespace
            };
            
            var publicProperties = apiContextType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (var property in publicProperties)
            {
                var entityClrType = TypeHelper.GetImplementedIEnumerableType(property.PropertyType);
                var entity = builder.AddEntityType(entityClrType);
                builder.AddEntitySet(property.Name, entity);
            }
			after?.Invoke(builder);
			var edmModel = builder.GetEdmModel();
            return edmModel;
		}
开发者ID:joshcomley,项目名称:WebApi,代码行数:18,代码来源:DefaultODataModelProvider.cs

示例5: MapAllODataModels

        private static ODataConventionModelBuilder MapAllODataModels(string namespaceForTypes)
        {
            var builder = new ODataConventionModelBuilder();

            var types =
                Assembly.GetExecutingAssembly()
                    .GetTypes()
                    .Where(x => string.Equals(x.Namespace, namespaceForTypes, StringComparison.Ordinal));

            foreach (var type in types)
            {
                var name = type.GetCustomAttribute(typeof (ApiMapNameAttribute)) as ApiMapNameAttribute;
                if (name == null) continue;

                var entityType = builder.AddEntityType(type);
                builder.AddEntitySet(name.Name, entityType);
            }

            return builder;
        }
开发者ID:alexserdyuk,项目名称:bvcms,代码行数:20,代码来源:WebApiConfig.cs

示例6: GenerateSelfLinkWithoutCast_Works

        public void GenerateSelfLinkWithoutCast_Works()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var vehicles = builder.AddEntitySet("cars", builder.AddEntity(typeof(Car)));

            IEdmModel model = builder.GetEdmModel();
            IEdmEntitySet carsEdmEntitySet = model.EntityContainers().Single().EntitySets().Single();

            HttpConfiguration configuration = new HttpConfiguration();
            configuration.Routes.MapHttpRoute(ODataRouteNames.GetById, "{controller}({id})");

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey] = new HttpRouteData(new HttpRoute());

            Uri uri =
                SelfLinksGenerationConvention.GenerateSelfLink(
                vehicles,
                new EntityInstanceContext(model, carsEdmEntitySet, carsEdmEntitySet.ElementType, request.GetUrlHelper(), new Car { Model = 2009, Name = "Accord" }),
                includeCast: false);

            Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')", uri.AbsoluteUri);
        }
开发者ID:cubski,项目名称:aspnetwebstack,代码行数:23,代码来源:SelfLinksGenerationConventionTest.cs

示例7: GetEdmModel

        internal static IEdmModel GetEdmModel(this HttpActionDescriptor actionDescriptor, Type entityClrType)
        {
            if (actionDescriptor == null)
            {
                throw Error.ArgumentNull("actionDescriptor");
            }

            if (entityClrType == null)
            {
                throw Error.ArgumentNull("entityClrType");
            }

            // save the EdmModel to the action descriptor
            return actionDescriptor.Properties.GetOrAdd(ModelKeyPrefix + entityClrType.FullName, _ =>
                {
                    ODataConventionModelBuilder builder =
                        new ODataConventionModelBuilder(actionDescriptor.Configuration, isQueryCompositionMode: true);
                    EntityTypeConfiguration entityTypeConfiguration = builder.AddEntityType(entityClrType);
                    builder.AddEntitySet(entityClrType.Name, entityTypeConfiguration);
                    IEdmModel edmModel = builder.GetEdmModel();
                    Contract.Assert(edmModel != null);
                    return edmModel;
                }) as IEdmModel;
        }
开发者ID:ZhaoYngTest01,项目名称:WebApi,代码行数:24,代码来源:HttpActionDescriptorExtensions.cs

示例8: GenerateNavigationLink_GeneratesCorrectLink_EvenIfRouteDataPointsToADifferentController

        public void GenerateNavigationLink_GeneratesCorrectLink_EvenIfRouteDataPointsToADifferentController()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var orders = builder.AddEntitySet("Orders", builder.AddEntity(typeof(NavigationLinksGenerationConventionTest_Order)));

            IEdmModel model = builder.GetEdmModel();
            var edmEntitySet = model.EntityContainers().Single().EntitySets().Single();

            HttpConfiguration configuration = new HttpConfiguration();
            configuration.EnableOData(model);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey] = new HttpRouteData(new HttpRoute(), new HttpRouteValueDictionary(new { controller = "Customers" }));

            Uri uri =
                NavigationLinksGenerationConvention.GenerateNavigationPropertyLink(
                new EntityInstanceContext
                {
                    EdmModel = model,
                    EntityInstance = new NavigationLinksGenerationConventionTest_Order { ID = 100 },
                    EntitySet = edmEntitySet,
                    EntityType = edmEntitySet.ElementType,
                    PathHandler = new DefaultODataPathHandler(model),
                    UrlHelper = request.GetUrlHelper()
                },
                edmEntitySet.ElementType.NavigationProperties().Single(),
                orders,
                includeCast: false);

            Assert.Equal("http://localhost/Orders(100)/Customer", uri.AbsoluteUri);
        }
开发者ID:Swethach,项目名称:aspnetwebstack,代码行数:31,代码来源:NavigationLinksGenerationConventionTest.cs

示例9: ObjectCollectionsAreIgnoredByDefault

        public void ObjectCollectionsAreIgnoredByDefault(Type propertyType)
        {
            MockType type =
                new MockType("entity")
                .Property<int>("ID")
                .Property(propertyType, "Collection");

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var entityType = builder.AddEntity(type);
            builder.AddEntitySet("entityset", entityType);

            IEdmModel model = builder.GetEdmModel();
            Assert.Equal(2, model.SchemaElements.Count());
            var entityEdmType = model.AssertHasEntitySet("entityset", type);
        }
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:15,代码来源:ODataConventionModelBuilderTests.cs

示例10: CanBuildModelForAnonymousTypes

        public void CanBuildModelForAnonymousTypes()
        {
            Type entityType = new
            {
                ID = default(int),
                NavigationCollection = new[]
                {
                    new { ID = default(int) }
                }
            }.GetType();

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder(new HttpConfiguration(), isQueryCompositionMode: true);
            builder.AddEntitySet("entityset", builder.AddEntity(entityType));

            IEdmModel model = builder.GetEdmModel();

            IEdmEntityType entity = model.AssertHasEntitySet("entityset", entityType);
            entity.AssertHasKey(model, "ID", EdmPrimitiveTypeKind.Int32);
            entity.AssertHasNavigationProperty(model, "NavigationCollection", new { ID = default(int) }.GetType(), isNullable: false, multiplicity: EdmMultiplicity.Many);
        }
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:20,代码来源:ODataConventionModelBuilderTests.cs

示例11: ModelBuilder_DerivedComplexTypeHavingKeys_SuccedsIfToldToBeComplex

        public void ModelBuilder_DerivedComplexTypeHavingKeys_SuccedsIfToldToBeComplex()
        {
            MockType baseComplexType = new MockType("BaseComplexType");

            MockType derivedComplexType =
                new MockType("DerivedComplexType")
                .Property(typeof(int), "DerivedComplexTypeId")
                .BaseType(baseComplexType);

            MockType entityType =
                new MockType("EntityType")
                .Property(typeof(int), "ID")
                .Property(baseComplexType.Object, "ComplexProperty");

            MockAssembly assembly = new MockAssembly(baseComplexType, derivedComplexType, entityType);

            HttpConfiguration configuration = new HttpConfiguration();
            configuration.Services.Replace(typeof(IAssembliesResolver), new TestAssemblyResolver(assembly));
            var builder = new ODataConventionModelBuilder(configuration);

            builder.AddEntitySet("entities", builder.AddEntity(entityType));
            builder.AddComplexType(baseComplexType);

            IEdmModel model = builder.GetEdmModel();
            Assert.Equal(3, model.SchemaElements.Count());
            Assert.NotNull(model.FindType("DefaultNamespace.EntityType"));
            Assert.NotNull(model.FindType("DefaultNamespace.BaseComplexType"));
        }
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:28,代码来源:ODataConventionModelBuilderTests.cs

示例12: ModelBuilder_DerivedComplexTypeHavingKeys_Throws

        public void ModelBuilder_DerivedComplexTypeHavingKeys_Throws()
        {
            MockType baseComplexType = new MockType("BaseComplexType");

            MockType derivedComplexType =
                new MockType("DerivedComplexType")
                .Property(typeof(int), "DerivedComplexTypeId")
                .BaseType(baseComplexType);

            MockType entityType =
                new MockType("EntityType")
                .Property(typeof(int), "ID")
                .Property(baseComplexType.Object, "ComplexProperty");

            MockAssembly assembly = new MockAssembly(baseComplexType, derivedComplexType, entityType);

            HttpConfiguration configuration = new HttpConfiguration();
            configuration.Services.Replace(typeof(IAssembliesResolver), new TestAssemblyResolver(assembly));
            var builder = new ODataConventionModelBuilder(configuration);

            builder.AddEntitySet("entities", builder.AddEntity(entityType));

            Assert.Throws<InvalidOperationException>(
                () => builder.GetEdmModel(),
                "Cannot define keys on type 'DefaultNamespace.DerivedComplexType' deriving from 'DefaultNamespace.BaseComplexType'. Only the root type in the entity inheritance hierarchy can contain keys.");
        }
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:26,代码来源:ODataConventionModelBuilderTests.cs

示例13: DerivedTypes_Can_DefineKeys_InQueryCompositionMode

        public void DerivedTypes_Can_DefineKeys_InQueryCompositionMode()
        {
            // Arrange
            MockType baseType =
                 new MockType("BaseType")
                 .Property(typeof(int), "ID");

            MockType derivedType =
                new MockType("DerivedType")
                .Property(typeof(int), "DerivedTypeId")
                .BaseType(baseType);

            MockAssembly assembly = new MockAssembly(baseType, derivedType);

            HttpConfiguration configuration = new HttpConfiguration();
            configuration.Services.Replace(typeof(IAssembliesResolver), new TestAssemblyResolver(assembly));
            var builder = new ODataConventionModelBuilder(configuration, isQueryCompositionMode: true);

            builder.AddEntitySet("bases", builder.AddEntity(baseType));

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            model.AssertHasEntitySet("bases", baseType);
            IEdmEntityType baseEntityType = model.AssertHasEntityType(baseType);
            IEdmEntityType derivedEntityType = model.AssertHasEntityType(derivedType, baseType);
            baseEntityType.AssertHasKey(model, "ID", EdmPrimitiveTypeKind.Int32);
            derivedEntityType.AssertHasPrimitiveProperty(model, "DerivedTypeId", EdmPrimitiveTypeKind.Int32, isNullable: false);
        }
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:30,代码来源:ODataConventionModelBuilderTests.cs

示例14: OnModelCreating_IsInvoked_AfterConventionsAreRun

        public void OnModelCreating_IsInvoked_AfterConventionsAreRun()
        {
            // Arrange
            MockType entity =
                new MockType("entity")
                .Property<int>("ID");

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.AddEntitySet("entities", builder.AddEntity(entity));
            builder.OnModelCreating = (modelBuilder) =>
                {
                    var entityConfiguration = modelBuilder.StructuralTypes.OfType<EntityTypeConfiguration>().Single();
                    Assert.Equal(1, entityConfiguration.Keys.Count());
                    var key = entityConfiguration.Keys.Single();
                    Assert.Equal("ID", key.Name);

                    // mark the key as optional just to verify later.
                    key.OptionalProperty = true;
                };

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            Assert.True(model.SchemaElements.OfType<IEdmEntityType>().Single().Key().Single().Type.IsNullable);
        }
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:26,代码来源:ODataConventionModelBuilderTests.cs

示例15: CanMapObjectArrayAsAComplexProperty

        public void CanMapObjectArrayAsAComplexProperty()
        {
            MockType type =
                new MockType("entity")
                .Property<int>("ID")
                .Property<object[]>("Collection");

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var entityType = builder.AddEntity(type);
            entityType.AddCollectionProperty(type.GetProperty("Collection"));
            builder.AddEntitySet("entityset", entityType);

            IEdmModel model = builder.GetEdmModel();
            Assert.Equal(3, model.SchemaElements.Count());
            var entityEdmType = model.AssertHasEntitySet("entityset", type);
            model.AssertHasComplexType(typeof(object));
            entityEdmType.AssertHasCollectionProperty(model, "Collection", typeof(object), isNullable: true);
        }
开发者ID:naulizzang,项目名称:aspnetwebstack,代码行数:18,代码来源:ODataConventionModelBuilderTests.cs


注:本文中的ODataConventionModelBuilder.AddEntitySet方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。