Пример #1
0
        private static Func <InternalEntityEntry, TProperty> CreateCurrentValueGetter <TProperty>(IPropertyBase propertyBase)
        {
            var entityClrType  = propertyBase.DeclaringEntityType.ClrType;
            var entryParameter = Expression.Parameter(typeof(InternalEntityEntry), "entry");

            var        shadowIndex = (propertyBase as IProperty)?.GetShadowIndex() ?? -1;
            Expression currentValueExpression;

            if (shadowIndex >= 0)
            {
                currentValueExpression = Expression.Call(
                    entryParameter,
                    InternalEntityEntry.ReadShadowValueMethod.MakeGenericMethod(typeof(TProperty)),
                    Expression.Constant(shadowIndex));
            }
            else
            {
                currentValueExpression = Expression.Property(
                    Expression.Convert(
                        Expression.Property(entryParameter, "Entity"),
                        entityClrType),
                    propertyBase.GetPropertyInfo());
            }

            var storeGeneratedIndex = propertyBase.GetStoreGeneratedIndex();

            if (storeGeneratedIndex >= 0)
            {
                currentValueExpression = Expression.Call(
                    entryParameter,
                    InternalEntityEntry.ReadStoreGeneratedValueMethod.MakeGenericMethod(typeof(TProperty)),
                    currentValueExpression,
                    Expression.Constant(storeGeneratedIndex));
            }

            return(Expression.Lambda <Func <InternalEntityEntry, TProperty> >(
                       currentValueExpression,
                       entryParameter)
                   .Compile());
        }