本文整理汇总了C#中Microsoft.Data.Edm.Library.EdmModel.SetAnnotationValue方法的典型用法代码示例。如果您正苦于以下问题:C# EdmModel.SetAnnotationValue方法的具体用法?C# EdmModel.SetAnnotationValue怎么用?C# EdmModel.SetAnnotationValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Data.Edm.Library.EdmModel
的用法示例。
在下文中一共展示了EdmModel.SetAnnotationValue方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: ToDictionary_ContainsAllStructuralProperties_IfInstanceIsNotNull
public void ToDictionary_ContainsAllStructuralProperties_IfInstanceIsNotNull()
{
// Arrange
EdmModel model = new EdmModel();
EdmEntityType entityType = new EdmEntityType("NS", "Name");
model.AddElement(entityType);
model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity)));
entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32);
IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false);
SelectExpandWrapper<TestEntity> testWrapper = new SelectExpandWrapper<TestEntity>
{
Instance = new TestEntity { SampleProperty = 42 },
ModelID = ModelContainer.GetModelID(model)
};
// Act
var result = testWrapper.ToDictionary();
// Assert
Assert.Equal(42, result["SampleProperty"]);
}
示例3: Property_EntityInstance_CanBeBuiltFromIEdmObject
public void Property_EntityInstance_CanBeBuiltFromIEdmObject()
{
// Arrange
EdmEntityType edmType = new EdmEntityType("NS", "Name");
edmType.AddStructuralProperty("Property", EdmPrimitiveTypeKind.Int32);
EdmModel model = new EdmModel();
model.AddElement(edmType);
model.SetAnnotationValue<ClrTypeAnnotation>(edmType, new ClrTypeAnnotation(typeof(TestEntity)));
Mock<IEdmEntityObject> edmObject = new Mock<IEdmEntityObject>();
object propertyValue = 42;
edmObject.Setup(e => e.TryGetPropertyValue("Property", out propertyValue)).Returns(true);
edmObject.Setup(e => e.GetEdmType()).Returns(new EdmEntityTypeReference(edmType, isNullable: false));
EntityInstanceContext entityContext = new EntityInstanceContext { EdmModel = model, EdmObject = edmObject.Object, EntityType = edmType };
// Act
object resource = entityContext.EntityInstance;
// Assert
TestEntity testEntity = Assert.IsType<TestEntity>(resource);
Assert.Equal(42, testEntity.Property);
}
示例4: CreateTypeNameExpression_ReturnsConditionalExpression_IfTypeHasDerivedTypes
public void CreateTypeNameExpression_ReturnsConditionalExpression_IfTypeHasDerivedTypes()
{
// Arrange
IEdmEntityType baseType = new EdmEntityType("NS", "BaseType");
IEdmEntityType typeA = new EdmEntityType("NS", "A", baseType);
IEdmEntityType typeB = new EdmEntityType("NS", "B", baseType);
IEdmEntityType typeAA = new EdmEntityType("NS", "AA", typeA);
IEdmEntityType typeAAA = new EdmEntityType("NS", "AAA", typeAA);
IEdmEntityType[] types = new[] { baseType, typeA, typeAAA, typeB, typeAA };
EdmModel model = new EdmModel();
foreach (var type in types)
{
model.AddElement(type);
model.SetAnnotationValue(type, new ClrTypeAnnotation(new MockType(type.Name, @namespace: type.Namespace)));
}
Expression source = Expression.Constant(42);
// Act
Expression result = SelectExpandBinder.CreateTypeNameExpression(source, baseType, model);
// Assert
Assert.Equal(
result.ToString(),
@"IIF((42 Is AAA), ""NS.AAA"", IIF((42 Is AA), ""NS.AA"", IIF((42 Is B), ""NS.B"", IIF((42 Is A), ""NS.A"", ""NS.BaseType""))))");
}
示例5: Property_EntityInstance_EdmObjectHasCollectionProperty
public void Property_EntityInstance_EdmObjectHasCollectionProperty()
{
// Arrange
EdmEntityType edmType = new EdmEntityType("NS", "Name");
edmType.AddStructuralProperty(
"CollectionProperty",
new EdmCollectionTypeReference(
new EdmCollectionType(EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false)), isNullable: false));
EdmModel model = new EdmModel();
model.AddElement(edmType);
model.SetAnnotationValue<ClrTypeAnnotation>(edmType, new ClrTypeAnnotation(typeof(TestEntity)));
Mock<IEdmStructuredObject> edmObject = new Mock<IEdmStructuredObject>();
object propertyValue = new List<int> { 42 };
edmObject.Setup(e => e.TryGetValue("CollectionProperty", out propertyValue)).Returns(true);
edmObject.Setup(e => e.GetEdmType()).Returns(new EdmEntityTypeReference(edmType, isNullable: false));
EntityInstanceContext entityContext = new EntityInstanceContext { EdmModel = model, EdmObject = edmObject.Object, EntityType = edmType };
// Act
object resource = entityContext.EntityInstance;
// Assert
TestEntity testEntity = Assert.IsType<TestEntity>(resource);
Assert.Equal(new[] { 42 }, testEntity.CollectionProperty);
}
示例6: BuildEdmModel
public static IEdmModel BuildEdmModel(string containerNamespace, string containerName, IEnumerable<IStructuralTypeConfiguration> entityTypeConfigurations, IEnumerable<IEntitySetConfiguration> entitySetConfigurations)
{
if (entityTypeConfigurations == null)
{
throw Error.ArgumentNull("entityTypeConfigurations");
}
if (entitySetConfigurations == null)
{
throw Error.ArgumentNull("entitySetConfigurations");
}
EdmModel model = new EdmModel();
Dictionary<string, IEdmStructuredType> types = EdmTypeBuilder.GetTypes(entityTypeConfigurations)
.OfType<IEdmStructuredType>()
.ToDictionary(t => t.ToString());
foreach (IEdmStructuredType type in types.Values)
{
if (type.TypeKind == EdmTypeKind.Complex)
{
model.AddElement(type as IEdmComplexType);
}
else if (type.TypeKind == EdmTypeKind.Entity)
{
model.AddElement(type as IEdmEntityType);
}
else
{
throw Error.InvalidOperation(SRResources.UnsupportedEntityTypeInModel);
}
}
if (entitySetConfigurations.Any())
{
EdmEntityContainer container = new EdmEntityContainer(containerNamespace, containerName);
Dictionary<string, EdmEntitySet> edmEntitySetMap = new Dictionary<string, EdmEntitySet>();
foreach (IEntitySetConfiguration entitySet in entitySetConfigurations)
{
EdmEntitySet edmEntitySet = container.AddEntitySet(entitySet.Name, (IEdmEntityType)types[entitySet.EntityType.FullName]);
EntitySetLinkBuilderAnnotation entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(entitySet);
model.SetEntitySetLinkBuilderAnnotation(edmEntitySet, entitySetLinkBuilderAnnotation);
model.SetAnnotationValue<EntitySetUrlAnnotation>(edmEntitySet, new EntitySetUrlAnnotation { Url = entitySet.GetUrl() });
edmEntitySetMap.Add(edmEntitySet.Name, edmEntitySet);
}
foreach (IEntitySetConfiguration entitySet in entitySetConfigurations)
{
EdmEntitySet edmEntitySet = edmEntitySetMap[entitySet.Name];
EntitySetLinkBuilderAnnotation entitySetLinkBuilderAnnotation = model.GetEntitySetLinkBuilder(edmEntitySet) as EntitySetLinkBuilderAnnotation;
foreach (NavigationPropertyConfiguration navigation in entitySet.EntityType.NavigationProperties)
{
NavigationPropertyBinding binding = entitySet.FindBinding(navigation);
if (binding != null)
{
EdmEntityType edmEntityType = types[entitySet.EntityType.FullName] as EdmEntityType;
IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties().Single(np => np.Name == navigation.Name);
edmEntitySet.AddNavigationTarget(edmNavigationProperty, edmEntitySetMap[binding.EntitySet.Name]);
entitySetLinkBuilderAnnotation.AddNavigationPropertyLinkBuilder(edmNavigationProperty, entitySet.GetNavigationPropertyLink(edmNavigationProperty.Name));
}
}
}
model.AddElement(container);
}
return model;
}
示例7: CustomersModelWithInheritance
public CustomersModelWithInheritance()
{
EdmModel model = new EdmModel();
// complex type address
EdmComplexType address = new EdmComplexType("NS", "Address");
address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
address.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
address.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
address.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String);
model.AddElement(address);
// entity type customer
EdmEntityType customer = new EdmEntityType("NS", "Customer");
customer.AddKeys(customer.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
customer.AddStructuralProperty("Address", new EdmComplexTypeReference(address, isNullable: true));
model.AddElement(customer);
// derived entity type special customer
EdmEntityType specialCustomer = new EdmEntityType("NS", "SpecialCustomer", customer);
specialCustomer.AddStructuralProperty("SpecialCustomerProperty", EdmPrimitiveTypeKind.Guid);
model.AddElement(specialCustomer);
// entity type order
EdmEntityType order = new EdmEntityType("NS", "Order");
order.AddKeys(order.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
order.AddStructuralProperty("Amount", EdmPrimitiveTypeKind.Int32);
model.AddElement(order);
// derived entity type special order
EdmEntityType specialOrder = new EdmEntityType("NS", "SpecialOrder", order);
specialOrder.AddStructuralProperty("SpecialOrderProperty", EdmPrimitiveTypeKind.Guid);
model.AddElement(specialOrder);
// entity sets
EdmEntityContainer container = new EdmEntityContainer("NS", "ModelWithInheritance");
model.AddElement(container);
EdmEntitySet customers = container.AddEntitySet("Customers", customer);
EdmEntitySet orders = container.AddEntitySet("Orders", order);
// actions
EdmFunctionImport upgradeCustomer = container.AddFunctionImport(
"upgrade", returnType: null, entitySet: new EdmEntitySetReferenceExpression(customers), sideEffecting: true, composable: false, bindable: true);
upgradeCustomer.AddParameter("entity", new EdmEntityTypeReference(customer, false));
EdmFunctionImport upgradeSpecialCustomer = container.AddFunctionImport(
"specialUpgrade", returnType: null, entitySet: new EdmEntitySetReferenceExpression(customers), sideEffecting: true, composable: false, bindable: true);
upgradeSpecialCustomer.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false));
// navigation properties
customers.AddNavigationTarget(
customer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
{
Name = "Orders",
TargetMultiplicity = EdmMultiplicity.Many,
Target = order
}),
orders);
orders.AddNavigationTarget(
order.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
{
Name = "Customer",
TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
Target = customer
}),
customers);
// navigation properties on derived types.
customers.AddNavigationTarget(
specialCustomer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
{
Name = "SpecialOrders",
TargetMultiplicity = EdmMultiplicity.Many,
Target = order
}),
orders);
orders.AddNavigationTarget(
specialOrder.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
{
Name = "SpecialCustomer",
TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
Target = customer
}),
customers);
model.SetAnnotationValue<BindableProcedureFinder>(model, new BindableProcedureFinder(model));
// set properties
Model = model;
Container = container;
Customer = customer;
Order = order;
Address = address;
SpecialCustomer = specialCustomer;
SpecialOrder = specialOrder;
Orders = orders;
Customers = customers;
UpgradeCustomer = upgradeCustomer;
}
示例8: ToDictionary_AppliesMappingToAllProperties_IfInstanceIsNotNull
public void ToDictionary_AppliesMappingToAllProperties_IfInstanceIsNotNull()
{
// Arrange
EdmEntityType entityType = new EdmEntityType("NS", "Name");
entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32);
EdmModel model = new EdmModel();
model.AddElement(entityType);
model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity)));
IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false);
SelectExpandWrapper<TestEntity> testWrapper = new SelectExpandWrapper<TestEntity>
{
Instance = new TestEntity { SampleProperty = 42 },
ModelID = ModelContainer.GetModelID(model)
};
Mock<IPropertyMapper> mapperMock = new Mock<IPropertyMapper>();
mapperMock.Setup(m => m.MapProperty("SampleProperty")).Returns("Sample");
Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider =
(IEdmModel m, IEdmStructuredType t) => mapperMock.Object;
// Act
var result = testWrapper.ToDictionary(mapperProvider);
// Assert
Assert.Equal(42, result["Sample"]);
}
示例9: ToDictionary_Throws_IfMappingIsNullOrEmpty_ForAGivenProperty
public void ToDictionary_Throws_IfMappingIsNullOrEmpty_ForAGivenProperty(string propertyMapping)
{
// Arrange
EdmEntityType entityType = new EdmEntityType("NS", "Name");
entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32);
EdmModel model = new EdmModel();
model.AddElement(entityType);
model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity)));
IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false);
SelectExpandWrapper<TestEntity> testWrapper = new SelectExpandWrapper<TestEntity>
{
Instance = new TestEntity { SampleProperty = 42 },
ModelID = ModelContainer.GetModelID(model)
};
Mock<IPropertyMapper> mapperMock = new Mock<IPropertyMapper>();
mapperMock.Setup(m => m.MapProperty("SampleProperty")).Returns(propertyMapping);
Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider =
(IEdmModel m, IEdmStructuredType t) => mapperMock.Object;
// Act & Assert
Assert.Throws<InvalidOperationException>(() =>
testWrapper.ToDictionary(mapperProvider),
"The key mapping for the property 'SampleProperty' can't be null or empty.");
}
示例10: ToDictionary_Throws_IfMapperProvider_ReturnsNullPropertyMapper
public void ToDictionary_Throws_IfMapperProvider_ReturnsNullPropertyMapper()
{
// Arrange
EdmEntityType entityType = new EdmEntityType("NS", "Name");
entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32);
EdmModel model = new EdmModel();
model.AddElement(entityType);
model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity)));
IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false);
SelectExpandWrapper<TestEntity> wrapper = new SelectExpandWrapper<TestEntity>
{
Instance = new TestEntity { SampleProperty = 42 },
ModelID = ModelContainer.GetModelID(model)
};
Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider =
(IEdmModel m, IEdmStructuredType t) => null;
// Act & Assert
Assert.Throws<InvalidOperationException>(() =>
wrapper.ToDictionary(mapperProvider: mapperProvider),
"The mapper provider must return a valid 'System.Web.Http.OData.Query.IPropertyMapper' instance for the given 'NS.Name' IEdmType.");
}
示例11: ToDictionary_ContainsAllProperties_FromContainer
public void ToDictionary_ContainsAllProperties_FromContainer()
{
// Arrange
EdmEntityType entityType = new EdmEntityType("NS", "Name");
entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32);
EdmModel model = new EdmModel();
model.AddElement(entityType);
model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity)));
MockPropertyContainer container = new MockPropertyContainer();
container.Properties.Add("Property", 42);
SelectExpandWrapper<TestEntity> wrapper = new SelectExpandWrapper<TestEntity>
{
Container = container,
ModelID = ModelContainer.GetModelID(model)
};
// Act
var result = wrapper.ToDictionary();
// Assert
Assert.Equal(42, result["Property"]);
}