0

I have a User class :

public class User : BaseModel
    {
        // id in basemodel


        public string MobileNumber { get; set; }


        public string Password { get; set; }

        public string FullName {get; set;}    

        public StatusType Status { get; set; }


        public string Email { get; set; }



        [ScaffoldColumn(false)]
        public byte[] Key { get; set; }

        [ScaffoldColumn(false)]
        public byte[] Iv { get; set; }

        [ScaffoldColumn(false)]
        public byte[] Salt { get; set; }
        //Encrypt and Decrypt Properties
        [NotMapped]
        public string EnDecryptedMobileNumber
        {
            //get { return EnDeCryptMethods.AesDecrypt(this.MobileNumber, this.Key, this.Iv); }
            //set { this.MobileNumber = EnDeCryptMethods.AesEncrypt(value, this.Key, this.Iv); }
            get { return MobileNumber; }
            set { MobileNumber = value; }
        }
        [NotMapped]
        public string EnDecryptedPassword
        {
            //get { return EnDeCryptMethods.AesDecrypt(this.Password, this.Key, this.Iv); }
            //set { this.Password = EnDeCryptMethods.AesEncrypt(value, this.Key, this.Iv); }
            get { return this.Password; }
            set { this.Password = value; }
        }
        [NotMapped]
        public string EnDecryptedEmail
        {
            //get { return EnDeCryptMethods.AesDecrypt(this.Email, this.Key, this.Iv); }
            //set { this.Email = EnDeCryptMethods.AesEncrypt(value, this.Key, this.Iv); }
            get { return this.Email; }
            set { this.Email = value; }
        }

        //END Encrypt and Decrypt Properties


        [ForeignKey("Role")]
        public long RoleId { get; set; }
        public Role Role { get; set; }
    }

As you see in the above codes , I have two type properties . first type is mapped properties and save data in database ,And the second type is NotMapped properties and get and set value of first type properties to encrypt and decrypt.

In my controller i needed searching data in database and use Expression like x=>x.FullName.Contains(searchWord) for search and works fine for mapped property

(IQueryable<User> List, int? PageId, int? PageCount) users;

            if (!string.IsNullOrWhiteSpace(searchWord))
            {
                (bool tryToConverted, byte? numberSplit, DateTime resualtDateTime) date;
                switch (searchField)
                {
                    case "Email":
                        users = _service.UserRepository.GetAll(id, null, new Expression<Func<User, bool>>[] { x => x.EnDecryptedEmail.Contains(searchWord) }, u => u.Role);
                        break;


                            return View(users.List);
                        }

                        break;
                        .
                        .
                        .

                    default:

                            users = _service.UserRepository.GetAll(id, null, new Expression<Func<User, bool>>[]{x=>x.FullName.Contains(searchWord)}, u => u.Role);

                        break;
            }

but when i use for not mapped properties (ex: users = _service.UserRepository.GetAll(id, null, new Expression<Func<User, bool>>[] { x => x.EnDecryptedEmail.Contains(searchWord) }, u => u.Role);) get me an error:

The specified type member 'EnDecryptedEmail' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

I know this is a fault because [NotMapped] attribute; But [NotMapped] property basis to a mapped property

Is there a way to fix it?

arman
  • 125
  • 1
  • 12

1 Answers1

1

You could try to provide the Encryption public as a static method and use it to compare with your encrypted value:

public class User : BaseModel
{
    // id in basemodel


    public string MobileNumber { get; set; }


    public string Password { get; set; }

    public string FullName {get; set;}    

    public StatusType Status { get; set; }


    public string Email { get; set; }



    [ScaffoldColumn(false)]
    public byte[] Key { get; set; }

    [ScaffoldColumn(false)]
    public byte[] Iv { get; set; }

    [ScaffoldColumn(false)]
    public byte[] Salt { get; set; }
    //Encrypt and Decrypt Properties
    [NotMapped]
    public string EnDecryptedMobileNumber
    {
        //get { return EnDeCryptMethods.AesDecrypt(this.MobileNumber, this.Key, this.Iv); }
        //set { this.MobileNumber = EnDeCryptMethods.AesEncrypt(value, this.Key, this.Iv); }
        get { return MobileNumber; }
        set { MobileNumber = value; }
    }
    [NotMapped]
    public string EnDecryptedPassword
    {
        //get { return EnDeCryptMethods.AesDecrypt(this.Password, this.Key, this.Iv); }
        //set { this.Password = EnDeCryptMethods.AesEncrypt(value, this.Key, this.Iv); }
        get { return this.Password; }
        set { this.Password = value; }
    }
    [NotMapped]
    public string EnDecryptedEmail
    {
        //get { return EnDeCryptMethods.AesDecrypt(this.Email, this.Key, this.Iv); }
        //set { this.Email = EnDeCryptMethods.AesEncrypt(value, this.Key, this.Iv); }
        get { return this.Email; }
        set { this.Email = value; }
    }

    //END Encrypt and Decrypt Properties


    [ForeignKey("Role")]
    public long RoleId { get; set; }
    public Role Role { get; set; }

    public static string GetEncryptedValue(string value)
    {
        // return ... your Encryption-Code;
     }
}

Then you can use it this way:

var encryptedValue = User.GetEncryptedValue(searchWord);
users = _service.UserRepository.GetAll(id, null, new Expression<Func<User, bool>>[] { x => x.Email.Contains(encryptedValue) }, u => u.Role);
Nikolaus
  • 1,859
  • 1
  • 10
  • 16