本文整理汇总了C#中ODataConventionModelBuilder.GetTypeConfigurationOrNull方法的典型用法代码示例。如果您正苦于以下问题:C# ODataConventionModelBuilder.GetTypeConfigurationOrNull方法的具体用法?C# ODataConventionModelBuilder.GetTypeConfigurationOrNull怎么用?C# ODataConventionModelBuilder.GetTypeConfigurationOrNull使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ODataConventionModelBuilder
的用法示例。
在下文中一共展示了ODataConventionModelBuilder.GetTypeConfigurationOrNull方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Register
public static void Register(HttpConfiguration httpConfiguration)
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.Namespace = "ODataV4TestService.Models";
builder.EntitySet<Product>("Products");
builder.EntitySet<SuppliedProduct>("SuppliedProducts");
builder.EntitySet<Supplier>("Suppliers");
builder.EntitySet<Product>("OtherProducts");
builder.ComplexType<Description>();
builder.EntityType<Product>()
.Action("Rate")
.Parameter<int>("Rating");
builder.EntityType<Product>().Collection
.Function("Best")
.ReturnsCollectionFromEntitySet<Product>("Products");
var funcConfig = builder
.EntityType<Product>()
.Function("RelatedProducts")
.SetBindingParameter("product", builder.GetTypeConfigurationOrNull(typeof(Product)))
//.AddParameter("start", new PrimitiveTypeConfiguration(builder, builder.GetTypeConfigurationOrNull(typeof(DateTimeOffset)), typeof(DateTimeOffset)).
.ReturnsCollectionFromEntitySet<Product>("Products");
funcConfig
.Parameter<DateTimeOffset>("start");
funcConfig
.Parameter<DateTimeOffset>("end");
//builder.Function("GetSalesTaxRate")
// .Returns<double>()
// .Parameter<int>("PostalCode");
builder.EntitySet<Account>("Accounts");
builder.EntityType<PaymentInstrument>()
.Collection
.Function("GetCount")
.Returns<int>()
.Parameter<string>("NameContains");
var model = builder.GetEdmModel();
var conventions = ODataRoutingConventions.CreateDefault();
conventions.Insert(0, new AttributeRoutingConvention(model, httpConfiguration));
var server = new BatchServer(httpConfiguration);
httpConfiguration.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: model,
pathHandler: new DefaultODataPathHandler(),
routingConventions: conventions,
batchHandler: new DefaultODataBatchHandler(server));
httpConfiguration.MessageHandlers.Add(new TracingMessageHandler());
}
示例2: GetModelAsync
/// <inheritdoc/>
public async Task<IEdmModel> GetModelAsync(ModelContext context, CancellationToken cancellationToken)
{
// This means user build a model with customized model builder registered as inner most
// Its element will be added to built model.
IEdmModel innerModel = null;
if (InnerModelBuilder != null)
{
innerModel = await InnerModelBuilder.GetModelAsync(context, cancellationToken);
}
var entitySetTypeMap = context.ResourceSetTypeMap;
if (entitySetTypeMap == null || entitySetTypeMap.Count == 0)
{
return innerModel;
}
// Collection of entity type and set name is set by EF now,
// and EF model producer will not build model any more
// Web Api OData conversion model built is been used here,
// refer to Web Api OData document for the detail conversions been used for model built.
var builder = new ODataConventionModelBuilder();
// This namespace is used by container
builder.Namespace = entitySetTypeMap.First().Value.Namespace;
MethodInfo method = typeof(ODataConventionModelBuilder)
.GetMethod("EntitySet", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
foreach (var pair in entitySetTypeMap)
{
// Build a method with the specific type argument
var specifiedMethod = method.MakeGenericMethod(pair.Value);
var parameters = new object[]
{
pair.Key
};
specifiedMethod.Invoke(builder, parameters);
}
entitySetTypeMap.Clear();
var entityTypeKeyPropertiesMap = context.ResourceTypeKeyPropertiesMap;
if (entityTypeKeyPropertiesMap != null)
{
foreach (var pair in entityTypeKeyPropertiesMap)
{
var edmTypeConfiguration = builder.GetTypeConfigurationOrNull(pair.Key) as EntityTypeConfiguration;
if (edmTypeConfiguration == null)
{
continue;
}
foreach (var property in pair.Value)
{
edmTypeConfiguration.HasKey(property);
}
}
entityTypeKeyPropertiesMap.Clear();
}
var model = (EdmModel)builder.GetEdmModel();
// Add all Inner model content into existing model
// When WebApi OData make conversion model builder accept an existing model, this can be removed.
if (innerModel != null)
{
foreach (var element in innerModel.SchemaElements)
{
if (!(element is EdmEntityContainer))
{
model.AddElement(element);
}
}
foreach (var annotation in innerModel.VocabularyAnnotations)
{
model.AddVocabularyAnnotation(annotation);
}
var entityContainer = (EdmEntityContainer)model.EntityContainer;
var innerEntityContainer = (EdmEntityContainer)innerModel.EntityContainer;
if (innerEntityContainer != null)
{
foreach (var entityset in innerEntityContainer.EntitySets())
{
if (entityContainer.FindEntitySet(entityset.Name) == null)
{
entityContainer.AddEntitySet(entityset.Name, entityset.EntityType());
}
}
foreach (var singleton in innerEntityContainer.Singletons())
{
if (entityContainer.FindEntitySet(singleton.Name) == null)
{
entityContainer.AddSingleton(singleton.Name, singleton.EntityType());
}
//.........这里部分代码省略.........