public override DbExpression Visit(DbNewInstanceExpression expression) { var arguments = expression.Arguments.Select(a => { var propertyArg = a as DbPropertyExpression; if (propertyArg != null && _typesToTrim.Contains(propertyArg.Property.TypeUsage.EdmType.Name)) { return(EdmFunctions.Trim(a)); } return(a); }); var temp = new List <DbExpression>(); foreach (var a in expression.Arguments) { var propertyArg = a as DbPropertyExpression; if (propertyArg != null && _typesToTrim.Contains(propertyArg.Property.TypeUsage.EdmType.Name)) { temp.Add(EdmFunctions.Trim(a)); } else { temp.Add(a); } } return(DbExpressionBuilder.New(expression.ResultType, temp)); }
public override DbExpression Visit(DbNewInstanceExpression expression) { var argments = expression.Arguments.Select(t => { if (t is DbPropertyExpression propertyArg && _typeToTrim.Contains(propertyArg.Property.TypeUsage.EdmType.Name)) { return(EdmFunctions.Trim(t)); } return(t); }); return(DbExpressionBuilder.New(expression.ResultType, argments)); }
public override DbExpression Visit(DbNewInstanceExpression expression) { var arguments = expression.Arguments.Select(a => { var propertyArg = a as DbPropertyExpression; if (propertyArg != null && _typesToTrim.Contains(propertyArg.Property.TypeUsage.EdmType.Name)) { return(EdmFunctions.Trim(a)); } return(a); }); return(DbExpressionBuilder.New(expression.ResultType, arguments)); }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { var insertCommand = interceptionContext.Result as DbInsertCommandTree; if (insertCommand != null) { List <DbModificationClause> finalSetClauses = new List <DbModificationClause>( (IEnumerable <DbModificationClause>)insertCommand.SetClauses.Select( a => { var dbSetClause = a as DbSetClause; if (dbSetClause != null) { var dbPropertyExpression = dbSetClause.Property as DbPropertyExpression; if (dbPropertyExpression != null) { var edmProperty = dbPropertyExpression.Property as EdmProperty; if (edmProperty != null && edmProperty.MaxLength != null && _typesToTrim.Contains(edmProperty.TypeName)) { var dbConstantExpression = dbSetClause.Value as DbConstantExpression; if (dbConstantExpression != null && dbConstantExpression.Value != null) { var value = dbConstantExpression.Value.ToString(); if (!string.IsNullOrEmpty(value) && value.Length > (int)edmProperty.MaxLength) { return(DbExpressionBuilder.SetClause( DbExpressionBuilder.Property( DbExpressionBuilder.Variable( insertCommand.Target.VariableType, insertCommand.Target.VariableName), dbPropertyExpression.Property.Name), EdmFunctions.Trim( dbConstantExpression.Value.ToString() .Substring(0, (int)edmProperty.MaxLength)))); } } } } } return(a); })); var newInsertCommand = new DbInsertCommandTree( insertCommand.MetadataWorkspace, insertCommand.DataSpace, insertCommand.Target, new ReadOnlyCollection <DbModificationClause>(finalSetClauses), insertCommand.Returning); interceptionContext.Result = newInsertCommand; } } var updateCommand = interceptionContext.Result as DbUpdateCommandTree; if (updateCommand != null) { List <DbModificationClause> finalSetClauses = new List <DbModificationClause>( (IEnumerable <DbModificationClause>)updateCommand.SetClauses.Select( a => { var dbSetClause = a as DbSetClause; if (dbSetClause != null) { var dbPropertyExpression = dbSetClause.Property as DbPropertyExpression; if (dbPropertyExpression != null) { var edmProperty = dbPropertyExpression.Property as EdmProperty; if (edmProperty != null && edmProperty.MaxLength != null && _typesToTrim.Contains(edmProperty.TypeName)) { var dbConstantExpression = dbSetClause.Value as DbConstantExpression; if (dbConstantExpression != null && dbConstantExpression.Value != null) { var value = dbConstantExpression.Value.ToString(); if (!string.IsNullOrEmpty(value) && value.Length > (int)edmProperty.MaxLength) { return(DbExpressionBuilder.SetClause( DbExpressionBuilder.Property( DbExpressionBuilder.Variable( updateCommand.Target.VariableType, updateCommand.Target.VariableName), dbPropertyExpression.Property.Name), EdmFunctions.Trim( dbConstantExpression.Value.ToString() .Substring(0, (int)edmProperty.MaxLength)))); } } } } } return(a); })); var newInsertCommand = new DbUpdateCommandTree( updateCommand.MetadataWorkspace, updateCommand.DataSpace, updateCommand.Target, updateCommand.Predicate, new ReadOnlyCollection <DbModificationClause>(finalSetClauses), updateCommand.Returning); interceptionContext.Result = newInsertCommand; } }