private EntityInfo RestoreOriginal(EntityInfo entityInfo)
        {
            // fk's can get cleared depending on the order in which deletions occur -
            // EF needs the original values of these fk's under certain circumstances - ( not sure entirely what these are).
            // so we restore the original fk values right before we attach the entity
            // shouldn't be any side effects because we delete it immediately after.
            // ??? Do concurrency values also need to be restored in some cases
            // This method restores more than it actually needs to because we don't
            // have metadata easily avail here, but usually a deleted entity will
            // not have much in the way of OriginalValues.
            if (entityInfo.OriginalValuesMap == null || entityInfo.OriginalValuesMap.Keys.Count == 0)
            {
                return(entityInfo);
            }
            var entity     = entityInfo.Entity;
            var entityType = entity.GetType();
            //var efEntityType = GetEntityType(ObjectContext.MetadataWorkspace, entityType);
            //var keyPropertyNames = efEntityType.KeyMembers.Select(km => km.Name).ToList();
            var keyPropertyNames = GetKeyProperties(entityType).Select(kp => kp.Name).ToList();
            var ovl = entityInfo.OriginalValuesMap.ToList();

            for (var i = 0; i < ovl.Count; i++)
            {
                var kvp      = ovl[i];
                var propName = kvp.Key;
                // keys should be ignored
                if (keyPropertyNames.Contains(propName))
                {
                    continue;
                }
                var pi = entityType.GetProperty(propName);
                // unmapped properties should be ignored.
                if (pi == null)
                {
                    continue;
                }
                var nnPropType = TypeFns.GetNonNullableType(pi.PropertyType);
                // presumption here is that only a predefined type could be a fk or concurrency property
                if (TypeFns.IsPredefinedType(nnPropType))
                {
                    SetPropertyValue(entity, propName, kvp.Value);
                }
            }

            return(entityInfo);
        }
        private static BasePredicate PredicateFromKeyValue(String key, Object value)
        {
            Operator op = Operator.FromString(key);

            if (op != null)
            {
                if (op.OpType == OperatorType.AndOr)
                {
                    var preds2 = PredicatesFromObject(value);
                    return(new AndOrPredicate(op, preds2));
                }
                else if (op.OpType == OperatorType.Unary)
                {
                    BasePredicate pred = PredicateFromObject(value);
                    return(new UnaryPredicate(op, pred));
                }
                else
                {
                    throw new Exception("Invalid operator in context: " + key);
                }
            }

            if (value == null || TypeFns.IsPredefinedType(value.GetType()))
            {
                return(new BinaryPredicate(BinaryOperator.Equals, key, value));
            }
            else if (value is IDictionary <string, object> && ((IDictionary <string, object>)value).ContainsKey("value"))
            {
                return(new BinaryPredicate(BinaryOperator.Equals, key, value));
            }

            if (!(value is Dictionary <string, object>))
            {
                throw new Exception("Unable to resolve value associated with key:" + key);
            }

            var preds = new List <BasePredicate>();
            var map   = (Dictionary <string, object>)value;


            foreach (var subKey in map.Keys)
            {
                Operator      subOp  = Operator.FromString(subKey);
                Object        subVal = map[subKey];
                BasePredicate pred;
                if (subOp != null)
                {
                    if (subOp.OpType == OperatorType.AnyAll)
                    {
                        BasePredicate subPred = PredicateFromObject(subVal);
                        pred = new AnyAllPredicate(subOp, key, subPred);
                    }
                    else if (subOp.OpType == OperatorType.Binary)
                    {
                        pred = new BinaryPredicate(subOp, key, subVal);
                    }
                    else
                    {
                        throw new Exception("Unable to resolve OperatorType for key: " + subKey);
                    }
                    // next line old check was for null not 'ContainsKey'
                }
                else if (subVal is IDictionary <string, object> && ((IDictionary <string, object>)subVal).ContainsKey("value"))
                {
                    pred = new BinaryPredicate(BinaryOperator.Equals, key, subVal);
                }
                else
                {
                    throw new Exception("Unable to resolve BasePredicate after: " + key);
                }
                preds.Add(pred);
            }
            return(CreateCompoundPredicate(preds));
        }
        private bool CannotBeNull(Expression expr)
        {
            var t = expr.Type;

            return(TypeFns.IsPredefinedType(t) && t != typeof(String));
        }
Example #4
0
        // will return either a PropBlock or a LitBlock
        public static BaseBlock CreateRHSBlock(object exprSource,
                                               Type entityType, DataType otherExprDataType)
        {
            if (exprSource == null)
            {
                return(new LitBlock(exprSource, otherExprDataType));
            }

            if (exprSource is string)
            {
                var source = (string)exprSource;
                if (entityType == null)
                {
                    // if entityType is unknown then assume that the rhs is a
                    // literal
                    return(new LitBlock(source, otherExprDataType));
                }

                if (PropertySignature.IsProperty(entityType, source))
                {
                    return(new PropBlock(source, entityType));
                }
                else
                {
                    return(new LitBlock(source, otherExprDataType));
                }
            }

            if (TypeFns.IsPredefinedType(exprSource.GetType()))
            {
                return(new LitBlock(exprSource, otherExprDataType));
            }

            if (exprSource is IDictionary <string, object> )
            {
                var exprMap = (IDictionary <string, object>)exprSource;
                // note that this is NOT the same a using get and checking for null
                // because null is a valid 'value'.
                if (!exprMap.ContainsKey("value"))
                {
                    throw new Exception(
                              "Unable to locate a 'value' property on: "
                              + exprMap.ToString());
                }
                var value = exprMap["value"];

                if (exprMap.ContainsKey("isProperty"))
                {
                    return(new PropBlock((string)value, entityType));
                }
                else
                {
                    var dt       = (string)exprMap["dataType"];
                    var dataType = (dt != null) ? DataType.FromName(dt) : otherExprDataType;
                    return(new LitBlock(value, dataType));
                }
            }

            if (exprSource is IList)
            {
                // right now this pretty much implies the values on an 'in' clause
                return(new LitBlock(exprSource, otherExprDataType));
            }

            if (TypeFns.IsEnumType(exprSource.GetType()))
            {
                return(new LitBlock(exprSource, otherExprDataType));
            }

            throw new Exception(
                      "Unable to parse the right hand side of this BinaryExpression: "
                      + exprSource.ToString());
        }