本文整理汇总了C#中ODataModelBuilder类的典型用法代码示例。如果您正苦于以下问题:C# ODataModelBuilder类的具体用法?C# ODataModelBuilder怎么用?C# ODataModelBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ODataModelBuilder类属于命名空间,在下文中一共展示了ODataModelBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Apply_SingleForeignKeyOnForeignKeyProperty_Works
public void Apply_SingleForeignKeyOnForeignKeyProperty_Works()
{
// Arrange
Type dependentType = typeof(SingleDependentEntity2);
ODataModelBuilder builder = new ODataModelBuilder();
builder.Entity<PrincipalEntity>().HasKey(p => p.PrincipalIntId);
EntityTypeConfiguration dependentEntity = builder.AddEntity(dependentType);
PropertyInfo expectPropertyInfo = dependentType.GetProperty("PrincipalId");
PrimitivePropertyConfiguration primitiveProperty = dependentEntity.AddProperty(expectPropertyInfo);
PropertyInfo propertyInfo = dependentType.GetProperty("Principal");
NavigationPropertyConfiguration navigation = dependentEntity.AddNavigationProperty(propertyInfo,
EdmMultiplicity.ZeroOrOne);
navigation.AddedExplicitly = false;
// Act
new ForeignKeyAttributeConvention().Apply(primitiveProperty, dependentEntity);
// Assert
PropertyInfo actualPropertyInfo = Assert.Single(navigation.DependentProperties);
Assert.Same(expectPropertyInfo, actualPropertyInfo);
Assert.Equal("PrincipalIntId", Assert.Single(navigation.PrincipalProperties).Name);
Assert.False(primitiveProperty.OptionalProperty);
}
示例2: GetEdmModel
private static IEdmModel GetEdmModel()
{
ODataModelBuilder builder = new ODataModelBuilder();
// Configure LimitedEntity
EntitySetConfiguration<LimitedEntity> limitedEntities = builder.EntitySet<LimitedEntity>("LimitedEntities");
limitedEntities.EntityType.HasKey(p => p.Id);
limitedEntities.EntityType.ComplexProperty(c => c.ComplexProperty);
limitedEntities.EntityType.CollectionProperty(c => c.ComplexCollectionProperty).IsNotCountable();
limitedEntities.EntityType.HasMany(l => l.EntityCollectionProperty).IsNotCountable();
limitedEntities.EntityType.CollectionProperty(cp => cp.Integers).IsNotCountable();
// Configure LimitedRelatedEntity
EntitySetConfiguration<LimitedRelatedEntity> limitedRelatedEntities =
builder.EntitySet<LimitedRelatedEntity>("LimitedRelatedEntities");
limitedRelatedEntities.EntityType.HasKey(p => p.Id);
limitedRelatedEntities.EntityType.CollectionProperty(p => p.ComplexCollectionProperty).IsNotCountable();
// Configure Complextype
ComplexTypeConfiguration<LimitedComplex> complexType = builder.ComplexType<LimitedComplex>();
complexType.CollectionProperty(p => p.Strings).IsNotCountable();
complexType.Property(p => p.Value);
complexType.CollectionProperty(p => p.SimpleEnums).IsNotCountable();
// Configure EnumType
EnumTypeConfiguration<SimpleEnum> enumType = builder.EnumType<SimpleEnum>();
enumType.Member(SimpleEnum.First);
enumType.Member(SimpleEnum.Second);
enumType.Member(SimpleEnum.Third);
enumType.Member(SimpleEnum.Fourth);
return builder.GetEdmModel();
}
示例3: ConstructorEmptyRawValueThrows
public void ConstructorEmptyRawValueThrows()
{
var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
Assert.Throws<ArgumentException>(() =>
new OrderByQueryOption(string.Empty, new ODataQueryContext(model, typeof(Customer))));
}
示例4: Apply
public void Apply(ProcedureConfiguration configuration, ODataModelBuilder model)
{
FunctionConfiguration function = configuration as FunctionConfiguration;
if (function == null || !function.IsBindable)
{
return;
}
// You only need to create links for bindable functions that bind to a single entity.
if (function.BindingParameter.TypeConfiguration.Kind == EdmTypeKind.Entity && function.GetFunctionLink() == null)
{
string bindingParamterType = function.BindingParameter.TypeConfiguration.FullName;
function.HasFunctionLink(entityContext =>
entityContext.GenerateFunctionLink(bindingParamterType, function.FullyQualifiedName, function.Parameters.Select(p => p.Name)),
followsConventions: true);
}
else if (function.BindingParameter.TypeConfiguration.Kind == EdmTypeKind.Collection && function.GetFeedFunctionLink() == null)
{
if (((CollectionTypeConfiguration)function.BindingParameter.TypeConfiguration).ElementType.Kind ==
EdmTypeKind.Entity)
{
string bindingParamterType = function.BindingParameter.TypeConfiguration.FullName;
function.HasFeedFunctionLink(
feedContext =>
feedContext.GenerateFunctionLink(bindingParamterType, function.FullyQualifiedName, function.Parameters.Select(p => p.Name)),
followsConventions: true);
}
}
}
示例5: NavigationSourceConfiguration
protected NavigationSourceConfiguration(ODataModelBuilder modelBuilder, EntityTypeConfiguration entityType, string name)
{
if (modelBuilder == null)
{
throw Error.ArgumentNull("modelBuilder");
}
if (entityType == null)
{
throw Error.ArgumentNull("entityType");
}
if (String.IsNullOrEmpty(name))
{
throw Error.ArgumentNullOrEmpty("name");
}
_modelBuilder = modelBuilder;
Name = name;
EntityType = entityType;
ClrType = entityType.ClrType;
_url = Name;
_editLinkBuilder = null;
_readLinkBuilder = null;
_navigationPropertyLinkBuilders = new Dictionary<NavigationPropertyConfiguration, NavigationLinkBuilder>();
_navigationPropertyBindings = new Dictionary<NavigationPropertyConfiguration, NavigationPropertyBindingConfiguration>();
}
示例6: BuildEdmModel
public static IEdmModel BuildEdmModel(ODataModelBuilder builder)
{
if (builder == null)
{
throw Error.ArgumentNull("builder");
}
EdmModel model = new EdmModel();
EdmEntityContainer container = new EdmEntityContainer(builder.Namespace, builder.ContainerName);
// add types and sets, building an index on the way.
Dictionary<Type, IEdmStructuredType> edmTypeMap = model.AddTypes(builder.StructuralTypes);
Dictionary<string, EdmEntitySet> edmEntitySetMap = model.AddEntitySets(builder, container, edmTypeMap);
// add procedures
model.AddProcedures(builder.Procedures, container, edmTypeMap, edmEntitySetMap);
// finish up
model.AddElement(container);
// build the map from IEdmEntityType to IEdmFunctionImport
model.SetAnnotationValue<BindableProcedureFinder>(model, new BindableProcedureFinder(model));
return model;
}
示例7: GetEdmModel
public static IEdmModel GetEdmModel()
{
if (_model == null)
{
ODataModelBuilder model = new ODataModelBuilder();
var people = model.EntitySet<FormatterPerson>("People");
people.HasFeedSelfLink(context => new Uri(context.Url.ODataLink(new EntitySetPathSegment(
context.EntitySet))));
people.HasIdLink(context =>
{
return context.Url.ODataLink(
new EntitySetPathSegment(context.EntitySet),
new KeyValuePathSegment(context.GetPropertyValue("PerId").ToString()));
},
followsConventions: false);
var person = people.EntityType;
person.HasKey(p => p.PerId);
person.Property(p => p.Age);
person.Property(p => p.MyGuid);
person.Property(p => p.Name);
person.ComplexProperty<FormatterOrder>(p => p.Order);
var order = model.ComplexType<FormatterOrder>();
order.Property(o => o.OrderAmount);
order.Property(o => o.OrderName);
_model = model.GetEdmModel();
}
return _model;
}
示例8: CanUseRelativeLinks
public void CanUseRelativeLinks()
{
var builder = new ODataModelBuilder()
.Add_Customer_EntityType()
.Add_Order_EntityType()
.Add_CustomerOrders_Relationship()
.Add_Customers_EntitySet()
.Add_Orders_EntitySet()
.Add_CustomerOrders_Binding();
var customersSet = builder.EntitySet<Customer>("Customers");
customersSet.HasEditLink(o => new Uri(string.Format("Customers({0})", o.EntityInstance.CustomerId), UriKind.Relative));
customersSet.FindBinding("Orders").HasLinkFactory(o => string.Format("Orders/ByCustomerId/{0}", ((Customer)o.EntityInstance).CustomerId));
var model = builder.GetEdmModel();
var container = model.FindDeclaredEntityContainer("Container");
var customerEdmEntitySet = container.FindEntitySet("Customers");
// TODO: Fix later, need to add a reference
//var entityContext = new EntityInstanceContext<Customer>(model, customerEdmEntitySet, (IEdmEntityType)customerEdmEntitySet.ElementType, null, new Customer { CustomerId = 24 });
//Assert.Equal("Customers", customersSet.GetUrl());
///Assert.Equal("Customers(24)", customersSet.GetEditLink(entityContext).ToString());
//Assert.Equal("Orders/ByCustomerId/24", customersSet.FindBinding("Orders").GetLink(entityContext));
}
示例9: Apply_KeyNameConventions_Works
public void Apply_KeyNameConventions_Works()
{
// Arrange
ODataModelBuilder builder = new ODataModelBuilder();
EntityTypeConfiguration principalEntity = builder.AddEntity(typeof(DiscoveryPrincipalEntity));
PropertyInfo propertyInfo = typeof(DiscoveryPrincipalEntity).GetProperty("DiscoveryPrincipalEntityId");
principalEntity.HasKey(propertyInfo);
EntityTypeConfiguration dependentEntity = builder.AddEntity(typeof(DiscoveryDependentEntity));
PropertyInfo expectPropertyInfo = typeof(DiscoveryDependentEntity).GetProperty("DiscoveryPrincipalEntityId");
dependentEntity.AddProperty(expectPropertyInfo);
PropertyInfo navigationPropertyInfo = typeof(DiscoveryDependentEntity).GetProperty("Principal");
NavigationPropertyConfiguration navigation = dependentEntity.AddNavigationProperty(navigationPropertyInfo,
EdmMultiplicity.One);
navigation.AddedExplicitly = false;
// Act
new ForeignKeyDiscoveryConvention().Apply(navigation, dependentEntity);
// Assert
PropertyInfo actualPropertyInfo = Assert.Single(navigation.DependentProperties);
Assert.Same(expectPropertyInfo, actualPropertyInfo);
PropertyInfo principalProperty = Assert.Single(navigation.PrincipalProperties);
Assert.Equal("DiscoveryPrincipalEntityId", principalProperty.Name);
}
示例10: Apply
public void Apply(IEntitySetConfiguration configuration, ODataModelBuilder model)
{
if (configuration == null)
{
throw Error.ArgumentNull("configuration");
}
// We only need to configure the EditLink by convention, ReadLink and IdLink both delegate to EditLink
if (configuration.GetEditLink() == null)
{
configuration.HasEditLink(entityContext =>
{
string routeName = SelfRouteName ?? ODataRouteNames.GetById;
string editlink = entityContext.UrlHelper.Link(
routeName,
new
{
controller = configuration.Name,
id = ConventionsHelpers.GetEntityKeyValue(entityContext, configuration.EntityType)
});
if (editlink == null)
{
throw Error.InvalidOperation(SRResources.GetByIdRouteMissingOrIncorrect, routeName);
}
return new Uri(editlink);
});
}
}
示例11: GetExplicitModel
public static IEdmModel GetExplicitModel(string singletonName)
{
ODataModelBuilder builder = new ODataModelBuilder();
// Define EntityType of Partner
var partner = builder.EntityType<Partner>();
partner.HasKey(p => p.ID);
partner.Property(p => p.Name);
var partnerCompany = partner.HasRequired(p => p.Company);
// Define Enum Type
var category = builder.EnumType<CompanyCategory>();
category.Member(CompanyCategory.IT);
category.Member(CompanyCategory.Communication);
category.Member(CompanyCategory.Electronics);
category.Member(CompanyCategory.Others);
// Define EntityType of Company
var company = builder.EntityType<Company>();
company.HasKey(p => p.ID);
company.Property(p => p.Name);
company.Property(p => p.Revenue);
company.EnumProperty(p => p.Category);
var companyPartners = company.HasMany(p => p.Partners);
companyPartners.IsNotCountable();
var companyBranches = company.CollectionProperty(p => p.Branches);
// Define Complex Type: Office
var office = builder.ComplexType<Office>();
office.Property(p => p.City);
office.Property(p => p.Address);
// Define Derived Type: SubCompany
var subCompany = builder.EntityType<SubCompany>();
subCompany.DerivesFrom<Company>();
subCompany.Property(p => p.Location);
subCompany.Property(p => p.Description);
subCompany.ComplexProperty(p => p.Office);
builder.Namespace = typeof(Partner).Namespace;
// Define PartnerSet and Company(singleton)
EntitySetConfiguration<Partner> partnersConfiguration = builder.EntitySet<Partner>("Partners");
partnersConfiguration.HasIdLink(c=>c.GenerateSelfLink(false), true);
partnersConfiguration.HasSingletonBinding(c => c.Company, singletonName);
Func<EntityInstanceContext<Partner>, IEdmNavigationProperty, Uri> link = (eic, np) => eic.GenerateNavigationPropertyLink(np, false);
partnersConfiguration.HasNavigationPropertyLink(partnerCompany, link, true);
partnersConfiguration.EntityType.Collection.Action("ResetDataSource");
SingletonConfiguration<Company> companyConfiguration = builder.Singleton<Company>(singletonName);
companyConfiguration.HasIdLink(c => c.GenerateSelfLink(false), true);
companyConfiguration.HasManyBinding(c => c.Partners, "Partners");
Func<EntityInstanceContext<Company>, IEdmNavigationProperty, Uri> linkFactory = (eic, np) => eic.GenerateNavigationPropertyLink(np, false);
companyConfiguration.HasNavigationPropertyLink(companyPartners, linkFactory, true);
companyConfiguration.EntityType.Action("ResetDataSource");
companyConfiguration.EntityType.Function("GetPartnersCount").Returns<int>();
return builder.GetEdmModel();
}
示例12: Apply
public void Apply(EntitySetConfiguration configuration, ODataModelBuilder model)
{
if (configuration == null)
{
throw Error.ArgumentNull("configuration");
}
// generate links without cast for declared and inherited navigation properties
foreach (EntityTypeConfiguration entity in configuration.EntityType.ThisAndBaseTypes())
{
foreach (NavigationPropertyConfiguration property in entity.NavigationProperties)
{
if (configuration.GetNavigationPropertyLink(property) == null)
{
configuration.HasNavigationPropertyLink(
property,
(entityContext, navigationProperty) => GenerateNavigationPropertyLink(entityContext, navigationProperty, configuration, includeCast: false));
}
}
}
// generate links with cast for navigation properties in derived types.
foreach (EntityTypeConfiguration entity in model.DerivedTypes(configuration.EntityType))
{
foreach (NavigationPropertyConfiguration property in entity.NavigationProperties)
{
if (configuration.GetNavigationPropertyLink(property) == null)
{
configuration.HasNavigationPropertyLink(
property,
(entityContext, navigationProperty) => GenerateNavigationPropertyLink(entityContext, navigationProperty, configuration, includeCast: true));
}
}
}
}
示例13: EntitySetConfiguration
public EntitySetConfiguration(ODataModelBuilder modelBuilder, EntityTypeConfiguration entityType, string name)
{
if (modelBuilder == null)
{
throw Error.ArgumentNull("modelBuilder");
}
if (entityType == null)
{
throw Error.ArgumentNull("entityType");
}
if (name == null)
{
throw Error.ArgumentNull("name");
}
_modelBuilder = modelBuilder;
Name = name;
EntityType = entityType;
ClrType = entityType.ClrType;
_url = Name;
_editLinkFactory = null;
_readLinkFactory = null;
_navigationPropertyLinkBuilders = new Dictionary<NavigationPropertyConfiguration, Func<EntityInstanceContext, IEdmNavigationProperty, Uri>>();
_entitySetBindings = new Dictionary<NavigationPropertyConfiguration, NavigationPropertyBinding>();
}
示例14: Apply_MultiForeignKeysOnNavigationProperty_Works
public void Apply_MultiForeignKeysOnNavigationProperty_Works()
{
// Arrange
Type dependentType = typeof(MultiDependentEntity);
ODataModelBuilder builder = new ODataModelBuilder();
builder.Entity<PrincipalEntity>().HasKey(p => new { p.PrincipalIntId, p.PrincipalStringId });
EntityTypeConfiguration dependentEntity = builder.AddEntity(dependentType);
PropertyInfo expectPropertyInfo1 = dependentType.GetProperty("PrincipalId1");
dependentEntity.AddProperty(expectPropertyInfo1);
PropertyInfo expectPropertyInfo2 = dependentType.GetProperty("PrincipalId2");
dependentEntity.AddProperty(expectPropertyInfo2);
PropertyInfo propertyInfo = typeof(MultiDependentEntity).GetProperty("Principal");
NavigationPropertyConfiguration navigation = dependentEntity.AddNavigationProperty(propertyInfo,
EdmMultiplicity.One);
navigation.AddedExplicitly = false;
// Act
new ForeignKeyAttributeConvention().Apply(navigation, dependentEntity);
// Assert
Assert.Equal(2, navigation.DependentProperties.Count());
Assert.Same(expectPropertyInfo1, navigation.DependentProperties.First());
Assert.Same(expectPropertyInfo2, navigation.DependentProperties.Last());
Assert.Equal(2, navigation.PrincipalProperties.Count());
Assert.Equal("PrincipalIntId", navigation.PrincipalProperties.First().Name);
Assert.Equal("PrincipalStringId", navigation.PrincipalProperties.Last().Name);
}
示例15: CanCreateFunctionWithNoArguments
public void CanCreateFunctionWithNoArguments()
{
// Arrange
// Act
ODataModelBuilder builder = new ODataModelBuilder();
builder.Namespace = "MyNamespace";
builder.ContainerName = "MyContainer";
FunctionConfiguration function = builder.Function("Format");
// Assert
Assert.Equal("Format", function.Name);
Assert.Equal(ProcedureKind.Function, function.Kind);
Assert.NotNull(function.Parameters);
Assert.Empty(function.Parameters);
Assert.Null(function.ReturnType);
Assert.False(function.IsSideEffecting);
Assert.False(function.IsComposable);
Assert.False(function.IsBindable);
Assert.False(function.SupportedInFilter);
Assert.False(function.SupportedInOrderBy);
Assert.Equal("MyContainer.Format", function.ContainerQualifiedName);
Assert.Equal("MyContainer.Format", function.FullName);
Assert.Equal("MyNamespace.MyContainer.Format", function.FullyQualifiedName);
Assert.NotNull(builder.Procedures);
Assert.Equal(1, builder.Procedures.Count());
}