本文整理汇总了C#中EntityInstanceContext类的典型用法代码示例。如果您正苦于以下问题:C# EntityInstanceContext类的具体用法?C# EntityInstanceContext怎么用?C# EntityInstanceContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
EntityInstanceContext类属于命名空间,在下文中一共展示了EntityInstanceContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CanConfigureAllLinksViaEditLink
public void CanConfigureAllLinksViaEditLink()
{
// Arrange
ODataModelBuilder builder = GetCommonModel();
var expectedEditLink = "http://server/service/Products(15)";
var products = builder.EntitySet<EntitySetLinkConfigurationTest_Product>("Products");
products.HasEditLink(c => new Uri(
string.Format(
"http://server/service/Products({0})",
c.EntityInstance.ID
)
));
var actor = builder.EntitySets.Single();
var model = builder.GetEdmModel();
var productType = model.SchemaElements.OfType<IEdmEntityType>().Single();
var productsSet = model.SchemaElements.OfType<IEdmEntityContainer>().Single().EntitySets().Single();
var productInstance = new EntitySetLinkConfigurationTest_Product { ID = 15 };
var entityContext = new EntityInstanceContext { EdmModel = model, EntitySet = productsSet, EntityType = productType, EntityInstance = productInstance, UrlHelper = new UrlHelper(new HttpRequestMessage()) };
var entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(actor);
// Act
var editLinkUri = entitySetLinkBuilderAnnotation.BuildEditLink(entityContext);
var readLinkUri = entitySetLinkBuilderAnnotation.BuildReadLink(entityContext);
var idLink = entitySetLinkBuilderAnnotation.BuildIdLink(entityContext);
// Assert
Assert.NotNull(editLinkUri);
Assert.Equal(expectedEditLink, editLinkUri.ToString());
Assert.NotNull(readLinkUri);
Assert.Equal(expectedEditLink, readLinkUri.ToString());
Assert.NotNull(idLink);
Assert.Equal(expectedEditLink, idLink);
}
示例2: GenerateActionLink
internal static Uri GenerateActionLink(EntityInstanceContext entityContext, ActionConfiguration action)
{
// the entity type the action is bound to.
EntityTypeConfiguration actionEntityType = action.BindingParameter.TypeConfiguration as EntityTypeConfiguration;
Contract.Assert(actionEntityType != null, "we have already verified that binding paramter type is entity");
List<ODataPathSegment> actionPathSegments = new List<ODataPathSegment>();
actionPathSegments.Add(new EntitySetPathSegment(entityContext.EntitySet));
actionPathSegments.Add(new KeyValuePathSegment(ConventionsHelpers.GetEntityKeyValue(entityContext)));
// generate link with cast if the entityset type doesn't match the entity type the action is bound to.
if (!entityContext.EntitySet.ElementType.IsOrInheritsFrom(entityContext.EdmModel.FindDeclaredType(actionEntityType.FullName)))
{
actionPathSegments.Add(new CastPathSegment(entityContext.EntityType));
}
actionPathSegments.Add(new ActionPathSegment(action.Name));
string actionLink = entityContext.Url.ODataLink(actionPathSegments);
if (actionLink == null)
{
return null;
}
return new Uri(actionLink);
}
示例3: CanConfigureAllLinksViaIdLink
public void CanConfigureAllLinksViaIdLink()
{
// Arrange
ODataModelBuilder builder = GetCommonModel();
var expectedEditLink = "http://server/service/Products(15)";
var products = builder.EntitySet<EntitySetLinkConfigurationTest_Product>("Products");
products.HasIdLink(c =>
string.Format(
"http://server/service/Products({0})",
c.GetPropertyValue("ID")
),
followsConventions: false);
var actor = builder.EntitySets.Single();
var model = builder.GetEdmModel();
var productType = model.SchemaElements.OfType<IEdmEntityType>().Single();
var productsSet = model.SchemaElements.OfType<IEdmEntityContainer>().Single().EntitySets().Single();
var productInstance = new EntitySetLinkConfigurationTest_Product { ID = 15 };
var serializerContext = new ODataSerializerContext { Model = model, EntitySet = productsSet };
var entityContext = new EntityInstanceContext(serializerContext, productType.AsReference(), productInstance);
var entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(actor);
// Act
var selfLinks = entitySetLinkBuilderAnnotation.BuildEntitySelfLinks(entityContext, ODataMetadataLevel.Default);
// Assert
Assert.NotNull(selfLinks.EditLink);
Assert.Equal(expectedEditLink, selfLinks.EditLink.ToString());
Assert.NotNull(selfLinks.ReadLink);
Assert.Equal(expectedEditLink, selfLinks.ReadLink.ToString());
Assert.NotNull(selfLinks.IdLink);
Assert.Equal(expectedEditLink, selfLinks.IdLink);
}
示例4: GenerateActionLink
internal static Uri GenerateActionLink(EntityInstanceContext entityContext, ActionConfiguration action)
{
// the entity type the action is bound to.
EntityTypeConfiguration actionEntityType = action.BindingParameter.TypeConfiguration as EntityTypeConfiguration;
Contract.Assert(actionEntityType != null, "we have already verified that binding paramter type is entity");
Dictionary<string, object> routeValues = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
routeValues.Add(LinkGenerationConstants.Controller, entityContext.EntitySet.Name);
routeValues.Add(LinkGenerationConstants.BoundId, ConventionsHelpers.GetEntityKeyValue(entityContext, actionEntityType));
routeValues.Add(LinkGenerationConstants.ODataAction, action.Name);
string routeName;
// generate link without cast if the entityset type matches the entity type the action is bound to.
if (entityContext.EntitySet.ElementType.IsOrInheritsFrom(entityContext.EdmModel.FindDeclaredType(actionEntityType.FullName)))
{
routeName = ODataRouteNames.InvokeBoundAction;
}
else
{
routeName = ODataRouteNames.InvokeBoundActionWithCast;
routeValues.Add(LinkGenerationConstants.Entitytype, entityContext.EntityType.FullName());
}
string actionLink = entityContext.UrlHelper.Link(routeName, routeValues);
if (actionLink == null)
{
return null;
}
else
{
return new Uri(actionLink);
}
}
示例5: Apply_Doesnot_Override_UserConfiguration
public void Apply_Doesnot_Override_UserConfiguration()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
var vehicles = builder.EntitySet<Vehicle>("vehicles");
var car = builder.AddEntityType(typeof(Car));
var paintAction = vehicles.EntityType.Action("Paint");
paintAction.HasActionLink(ctxt => new Uri("http://localhost/ActionTestWorks"), followsConventions: false);
ActionLinkGenerationConvention convention = new ActionLinkGenerationConvention();
convention.Apply(paintAction, builder);
IEdmModel model = builder.GetEdmModel();
var vehiclesEdmSet = model.EntityContainer.FindEntitySet("vehicles");
var carEdmType = model.FindDeclaredType("System.Web.OData.Builder.TestModels.Car") as IEdmEntityType;
var paintEdmAction =
model.GetAvailableProcedures(
model.FindDeclaredType("System.Web.OData.Builder.TestModels.Car") as IEdmEntityType).Single()
as IEdmAction;
Assert.NotNull(paintEdmAction);
HttpConfiguration configuration = new HttpConfiguration();
configuration.Routes.MapODataServiceRoute(model);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.SetConfiguration(configuration);
ActionLinkBuilder actionLinkBuilder = model.GetActionLinkBuilder(paintEdmAction);
var serializerContext = new ODataSerializerContext { Model = model, NavigationSource = vehiclesEdmSet, Url = request.GetUrlHelper() };
var entityContext = new EntityInstanceContext(serializerContext, carEdmType.AsReference(), new Car { Model = 2009, Name = "Contoso" });
Uri link = actionLinkBuilder.BuildActionLink(entityContext);
Assert.Equal("http://localhost/ActionTestWorks", link.AbsoluteUri);
}
示例6: GenerateSelfLink
internal static Uri GenerateSelfLink(EntitySetConfiguration configuration, EntityInstanceContext entityContext, bool includeCast)
{
string routeName;
Dictionary<string, object> routeValues = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
routeValues.Add(LinkGenerationConstants.Controller, configuration.Name);
routeValues.Add(LinkGenerationConstants.Id, ConventionsHelpers.GetEntityKeyValue(entityContext, configuration.EntityType));
if (includeCast)
{
routeName = ODataRouteNames.GetByIdWithCast;
routeValues.Add(LinkGenerationConstants.Entitytype, entityContext.EntityType.FullName());
}
else
{
routeName = ODataRouteNames.GetById;
}
string editLink = entityContext.UrlHelper.Link(routeName, routeValues);
if (editLink == null)
{
throw Error.InvalidOperation(SRResources.GetByIdRouteMissingOrIncorrect, routeName);
}
return new Uri(editLink);
}
示例7: Singleton_CanOnlyConfigureIdLinkViaIdLinkFactory
public void Singleton_CanOnlyConfigureIdLinkViaIdLinkFactory()
{
// Arrange
ODataModelBuilder builder = GetSingletonModel();
const string ExpectedEditLink = "http://server/service/Exchange";
var product = builder.Singleton<SingletonProduct>("Exchange");
product.HasIdLink(c => new Uri("http://server/service/Exchange"),
followsConventions: false);
var exchange = builder.Singletons.Single();
var model = builder.GetEdmModel();
var productType = model.SchemaElements.OfType<IEdmEntityType>().Single();
var singleton = model.SchemaElements.OfType<IEdmEntityContainer>().Single().FindSingleton("Exchange");
var singletonInstance = new SingletonProduct { ID = 15 };
var serializerContext = new ODataSerializerContext { Model = model, NavigationSource = singleton };
var entityContext = new EntityInstanceContext(serializerContext, productType.AsReference(), singletonInstance);
var linkBuilderAnnotation = new NavigationSourceLinkBuilderAnnotation(exchange);
// Act
var selfLinks = linkBuilderAnnotation.BuildEntitySelfLinks(entityContext, ODataMetadataLevel.FullMetadata);
// Assert
Assert.NotNull(selfLinks.IdLink);
Assert.Equal(ExpectedEditLink, selfLinks.IdLink.ToString());
Assert.Null(selfLinks.ReadLink);
Assert.Null(selfLinks.EditLink);
}
示例8: Convention_GeneratesUri_ForActionBoundToEntity
public void Convention_GeneratesUri_ForActionBoundToEntity()
{
// Arrange
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Customer>("Customers");
var action = builder.EntityType<Customer>().Action("MyAction");
action.Parameter<string>("param");
IEdmModel model = builder.GetEdmModel();
// Act
HttpConfiguration configuration = new HttpConfiguration();
configuration.MapODataServiceRoute("odata", "odata", model);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:123");
request.SetConfiguration(configuration);
request.ODataProperties().RouteName = "odata";
IEdmEntitySet customers = model.EntityContainer.FindEntitySet("Customers");
var edmType = model.SchemaElements.OfType<IEdmEntityType>().First(e => e.Name == "Customer");
var serializerContext = new ODataSerializerContext { Model = model, NavigationSource = customers, Url = request.GetUrlHelper() };
var entityContext = new EntityInstanceContext(serializerContext, edmType.AsReference(), new Customer { Id = 109 });
// Assert
var edmAction = model.SchemaElements.OfType<IEdmAction>().First(f => f.Name == "MyAction");
Assert.NotNull(edmAction);
ActionLinkBuilder actionLinkBuilder = model.GetActionLinkBuilder(edmAction);
Uri link = actionLinkBuilder.BuildActionLink(entityContext);
Assert.Equal("http://localhost:123/odata/Customers(109)/Default.MyAction", link.AbsoluteUri);
}
示例9: ODataSerializerContext
/// <summary>
/// Initializes a new instance of the <see cref="ODataSerializerContext"/> class.
/// </summary>
/// <param name="entity">The entity whose navigation property is being expanded.</param>
/// <param name="selectExpandClause">The <see cref="SelectExpandClause"/> for the navigation property being expanded.</param>
/// <param name="navigationProperty">The navigation property being expanded.</param>
/// <remarks>This constructor is used to construct the serializer context for writing expanded properties.</remarks>
public ODataSerializerContext(EntityInstanceContext entity, SelectExpandClause selectExpandClause, IEdmNavigationProperty navigationProperty)
{
if (entity == null)
{
throw Error.ArgumentNull("entity");
}
if (navigationProperty == null)
{
throw Error.ArgumentNull("navigationProperty");
}
ODataSerializerContext context = entity.SerializerContext;
Request = context.Request;
Url = context.Url;
EntitySet = context.EntitySet;
Model = context.Model;
Path = context.Path;
RootElementName = context.RootElementName;
SkipExpensiveAvailabilityChecks = context.SkipExpensiveAvailabilityChecks;
MetadataLevel = context.MetadataLevel;
ExpandedEntity = entity;
SelectExpandClause = selectExpandClause;
NavigationProperty = navigationProperty;
EntitySet = context.EntitySet.FindNavigationTarget(navigationProperty);
}
示例10: Ctor_ThatBuildsNestedContext_CopiesProperties
public void Ctor_ThatBuildsNestedContext_CopiesProperties()
{
// Arrange
CustomersModelWithInheritance model = new CustomersModelWithInheritance();
ODataSerializerContext context = new ODataSerializerContext
{
EntitySet = model.Customers,
MetadataLevel = ODataMetadataLevel.FullMetadata,
Model = model.Model,
Path = new ODataPath(),
Request = new HttpRequestMessage(),
RootElementName = "somename",
SelectExpandClause = new SelectExpandClause(new SelectItem[0], allSelected: true),
SkipExpensiveAvailabilityChecks = true,
Url = new UrlHelper()
};
EntityInstanceContext entity = new EntityInstanceContext { SerializerContext = context };
SelectExpandClause selectExpand = new SelectExpandClause(new SelectItem[0], allSelected: true);
IEdmNavigationProperty navProp = model.Customer.NavigationProperties().First();
// Act
ODataSerializerContext nestedContext = new ODataSerializerContext(entity, selectExpand, navProp);
// Assert
Assert.Equal(context.MetadataLevel, nestedContext.MetadataLevel);
Assert.Same(context.Model, nestedContext.Model);
Assert.Same(context.Path, nestedContext.Path);
Assert.Same(context.Request, nestedContext.Request);
Assert.Equal(context.RootElementName, nestedContext.RootElementName);
Assert.Equal(context.SkipExpensiveAvailabilityChecks, nestedContext.SkipExpensiveAvailabilityChecks);
Assert.Same(context.Url, nestedContext.Url);
}
示例11: GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType
public void GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType()
{
// Arrange
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
var cars = builder.EntitySet<Car>("cars");
var paintAction = cars.EntityType.Action("Paint");
IEdmModel model = builder.GetEdmModel();
var carsEdmSet = model.EntityContainers().Single().FindEntitySet("cars");
HttpConfiguration configuration = new HttpConfiguration();
string routeName = "Route";
configuration.Routes.MapODataRoute(routeName, null, model);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
request.SetConfiguration(configuration);
request.SetODataRouteName(routeName);
var serializerContext = new ODataSerializerContext { Model = model, EntitySet = carsEdmSet, Url = request.GetUrlHelper() };
var entityContext = new EntityInstanceContext(serializerContext, carsEdmSet.ElementType.AsReference(), new Car { Model = 2009, Name = "Accord" });
// Act
Uri link = ActionLinkGenerationConvention.GenerateActionLink(entityContext, paintAction);
Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/Paint", link.AbsoluteUri);
}
示例12: BuildEntitySelfLinks
public virtual EntitySelfLinks BuildEntitySelfLinks(EntityInstanceContext instanceContext, ODataMetadataLevel metadataLevel)
{
EntitySelfLinks selfLinks = new EntitySelfLinks();
selfLinks.IdLink = BuildIdLink(instanceContext, metadataLevel);
selfLinks.EditLink = BuildEditLink(instanceContext, metadataLevel, selfLinks.IdLink);
selfLinks.ReadLink = BuildReadLink(instanceContext, metadataLevel, selfLinks.EditLink);
return selfLinks;
}
示例13: BuildReadLink
public override Uri BuildReadLink(EntityInstanceContext instanceContext, ODataMetadataLevel metadataLevel, Uri editLink)
{
if (ReadLinkBuilder != null)
{
return ReadLinkBuilder.Factory(instanceContext);
}
return null;
}
示例14: BuildIdLink
public override string BuildIdLink(EntityInstanceContext instanceContext, ODataMetadataLevel metadataLevel)
{
if (IdLinkBuilder != null)
{
return IdLinkBuilder.Factory(instanceContext);
}
return null;
}
示例15: BuildEditLink
public override Uri BuildEditLink(EntityInstanceContext instanceContext, ODataMetadataLevel metadataLevel, string idLink)
{
if (EditLinkBuilder != null)
{
return EditLinkBuilder.Factory(instanceContext);
}
return null;
}