指定的类型成员' xxx' LINQ to Entities不支持。 [NotMapped]属性

时间:2017-11-21 15:40:39

标签: c# entity-framework linq

我有一个User类:

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; }
    }

正如您在上面的代码中看到的,我有两个类型属性。第一种类型是映射属性并将数据保存在数据库中,第二种类型是NotMapped属性,并获取和设置第一种类型属性的值以进行加密和解密。

在我的控制器中,我需要在数据库中搜索数据并使用像x=>x.FullName.Contains(searchWord)这样的表达式进行搜索,并且可以正常使用映射属性

(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;
            }

但当我用于未映射的属性时(例如:users = _service.UserRepository.GetAll(id, null, new Expression<Func<User, bool>>[] { x => x.EnDecryptedEmail.Contains(searchWord) }, u => u.Role);),请给我一个错误:

  

指定的类型成员&#39; EnDecryptedEmail&#39; LINQ不支持   到实体。仅初始化程序,实体成员和实体导航   支持属性。

我知道这是一个错误,因为[NotMapped]属性;但[NotMapped]属性基础为映射属性

有没有办法解决它?

1 个答案:

答案 0 :(得分:1)

您可以尝试将Encryption public作为静态方法提供,并使用它来与您的加密值进行比较:

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;
     }
}

然后你可以这样使用它:

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