Linq2db: ColumnAttribute.Storage property does not work as expected

Created on 29 Oct 2015  路  6Comments  路  Source: linq2db/linq2db

    public class EncryptedString
    {
        private string _str= "Encrypted";
        [Column(Storage = "_str")]
        public string Str { get { return "Decrypted"; } set { _str = "Encrypted"; } }
    }

The idea here is to save encrypted string to DB and decrypt it when using inside the code. Bit it does not work

db.Insert(new EncryptedString());

inserts "Decrypted", so storage is not used.

Also, when I query

db.Strings.Where(g => g.Str == "Decrypted")

It queries on "Decrypted" string, not "Encrypted"

Is it a bug? If not, how do I make it work?

feature

Most helpful comment

What about:

public class EncryptedString
{
    public string Encrypted;
    public string Value { get {return Decrypt(Encrypted); } set {Encrypted = Encrypt(value); } }
}

public MyEntity 
{
    public EncryptedString Value {get;set;}
}

MappigSchema.SetConvertExpression<EncryptedString, DataParameter>(s => return new DataParameter() { Value = s.Encrypted });
MappigSchema.SetConvertExpression<string, EncryptedString>(s => return new EncryptedString() { Value = s});

this should work

Speaking about your suggestion, i think yes we should implement also read usage for storage.

All 6 comments

did you see that \linq2db-master\Tests\Linq\Linq\ExplicitInterfaceTest.cs ?

@10der I need it to work in inserts and "where" and it's not

What about:

public class EncryptedString
{
    public string Encrypted;
    public string Value { get {return Decrypt(Encrypted); } set {Encrypted = Encrypt(value); } }
}

public MyEntity 
{
    public EncryptedString Value {get;set;}
}

MappigSchema.SetConvertExpression<EncryptedString, DataParameter>(s => return new DataParameter() { Value = s.Encrypted });
MappigSchema.SetConvertExpression<string, EncryptedString>(s => return new EncryptedString() { Value = s});

this should work

Speaking about your suggestion, i think yes we should implement also read usage for storage.

I have probably a similar Problem. I have to work with a 3rd party Database. They have a special case for "birthday"-fields. They save them in the database as a string in the format "MM-dd-yyyy".
In the Code i want to work with it, like it was a DateTime. Similar to this Question, because Scratch-net wants to work with the decrypted Value (DateTime in my case) in code, but on Queries and Inserts he needs the Encrypted Value ("MM-dd-yyyy"-string in my case).

If Storage could do that, that would be lovely. If i had to use the Workaround from here, i had to implement a "BirthdayFieldClass" and coudn't work with DateTime directly.

Or is there another Solution to my Problem? Maybe a "ConverterAttribute" or Something, which is only on some Fields?

If i understand the Converter "ms.SetValueToSqlConverter(typeof(DateTime), ... (from #1663 )" correctly, i can't use it, because in my case it is not for every DateTime, only for some fields.

Thanks for the help :)

Working on value converters, similar to HasConversion in EF Core. I hope will finish today.

@sdanyliv Sounds great! Would love to have that. 馃憤

Was this page helpful?
0 / 5 - 0 ratings