MikaelEliasson / EntityFramework.Utilities

Provides extensions for EntityFramework that doesn't exist out of the box like delete and update by query and bulk inserts
443 stars 175 forks source link

Handling complex types in UpdateAll() #100

Open bernd70 opened 8 years ago

bernd70 commented 8 years ago

Hi,

I encountered a problem when trying to update an value contained in a complex type with UpdateAll(). The generated SQL statement is invalid, the "SET ..." portion is empty.

I tracked it down to GetPropertyName() in ExpressionHelper.cs. Here only the Member name is returned but not its fully qualified name if it is a complex type.

So I adjusted the function to return the fully qualified name of the member. This seems to work for me but since I am not using a lot of EFUtilities functions I am not 100% sure if my change has other (bad :-)) implications.

So here is my code for discussion:

` public static string GetPropertyName<TSource, TProperty>(this Expression<Func<TSource, TProperty>> propertyLambda) { Type type = typeof(TSource);

        var temp = propertyLambda.Body;
        while (temp is UnaryExpression)
        {
            temp = (temp as UnaryExpression).Operand;
        }

        return GetFullyQualifiedPropertyName(temp as MemberExpression);
    }

    private static string GetFullyQualifiedPropertyName(MemberExpression member)
    {
        MemberExpression parentMember = member.Expression as MemberExpression;

        if (parentMember != null)
            return GetFullyQualifiedPropertyName(parentMember) + "." + member.Member.Name;

        return member.Member.Name;
    }`

Bernd