本文整理汇总了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;
}
示例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();
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
示例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;
}
示例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);
}
示例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);
}
示例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);
}
示例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"));
}
示例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.");
}
示例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);
}
示例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);
}
示例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);
}