efcore icon indicating copy to clipboard operation
efcore copied to clipboard

flags enum property where clause not affect on sql query

Open MohammadAkbari opened this issue 10 years ago • 3 comments

For assign mutiple type to user, I use UserType enum that decorate with Flags attribute as fallow:

[Flags]
public enum UserType
{
    Default = 0,
    Moderator = 1,
    Doctor = 2,
    Administrator = 4,
}
public class User : IdentityUser
{
    public bool Suspended { get; set; }
    public bool Deleted { get; set; }
    public UserType UserType { get; set; }
}

And assign userType to user,

user.UserType = UserType.Moderator | UserType.Administrator;

when below query runs,

var t = dbContext.Users.Where(e => e.UserType.HasFlag(UserType.Doctor)).ToList();

This sql query generate, as can be seen there is no trace of where expression in query. However, ef query result is ok, but the problem is that all User load to memroy and no filter occur in database, indeed query occur in memory

SELECT [e].[Id], [e].[PasswordHash], [e].[PhoneNumber], [e].[PhoneNumberConfirmed]
, [e].[SecurityStamp], [e].[Suspended], [e].[TwoFactorEnabled],[e].[UserName], [e].[UserType]
FROM [AspNetUsers] AS [e]

this problem not exists in ef 6.

MohammadAkbari avatar Jun 21 '16 07:06 MohammadAkbari

@smitpatel did you already add bitwise support?

rowanmiller avatar Jun 21 '16 18:06 rowanmiller

Support for bitwise operations is already in. Though the where clause is not generated here due to Enum.HasFlag is not implemented yet. dupe of #2852

smitpatel avatar Jun 21 '16 18:06 smitpatel

Thanks

rowanmiller avatar Jun 21 '16 19:06 rowanmiller