public static void SetAnnotation <TEntity>( this EdmModel model, IEdmTerm term, IEdmExpression expression, Expression <Func <TEntity, object> > propertyExpression = null) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var type = model.GetEdmType(typeof(TEntity)) as EdmEntityType; IEdmVocabularyAnnotatable target = type; if (propertyExpression != null) { var property = type.Properties().Single(p => p.Name == propertyExpression.GetAccessedProperty().Name); target = property; } var label = new EdmLabeledExpression("Value", expression); //var coll1 = new EdmCollectionExpression(new EdmStringConstant("test1"), new EdmStringConstant("test2"), new EdmStringConstant("test3"), label); //var coll = new EdmCollectionExpression(expression, coll1); var annotation = new EdmVocabularyAnnotation(target, term, label); annotation.SetSerializationLocation(model, target.ToSerializationLocation()); model.AddVocabularyAnnotation(annotation); }
public static void SetValidationAnnotation <TEntity>( this EdmModel model, IEdmTerm term, IEdmExpression expression, string message, Expression <Func <TEntity, bool> > validationExpression = null, Expression <Func <TEntity, object> > propertyExpression = null) { if (model == null) { throw new ArgumentNullException(nameof(model)); } // TODO: If validation expression is null, then compile it from the IQL var type = model.GetEdmType(typeof(TEntity)) as EdmEntityType; IEdmVocabularyAnnotatable target = type; string propertyName = null; if (propertyExpression != null) { propertyName = propertyExpression.GetAccessedProperty().Name; var property = type.Properties().Single(p => p.Name == propertyName); target = property; } IqlQueryableAdapter.ExpressionConverter = () => new ExpressionToIqlConverter(); //var iql = ExpressionToIqlExpressionParser<TEntity>.Parse(validationExpression); //var parser = // new ActionParserInstance<ODataIqlData, ODataIqlExpressionAdapter>(new ODataIqlExpressionAdapter()); var expressionLabel = new EdmLabeledExpression("Expression", expression); var messageLabel = new EdmLabeledExpression("Message", new EdmStringConstant(message)); var coll = new EdmCollectionExpression(expressionLabel, messageLabel); var annotation = new EdmVocabularyAnnotation(target, term, coll); var validation = ValidationMap.ForModel(model); var validationObject = new EntityValidation <TEntity>(validationExpression, message); validation.EntityValidation <TEntity>() .AddValidation(validationObject, propertyName); annotation.SetSerializationLocation(model, target.ToSerializationLocation()); model.AddVocabularyAnnotation(annotation); }