简体   繁体   中英

map field types in Servicestack.OrmLite

Lets say I have a field in the DB with possible values 'Y', 'N', NULL. I want to represent it in my DTO with a boolean property ('N', NULL -> false, 'Y' - > true). Can I somehow plug into OrmLite to make the conversion myself (both directions, ofc) during the query/update/insert? Thanks

ORMLite uses simple POCOs so you can do something like this:

public class MyDto
{
    // Values: Y, N, and NULL
    public string SomeDbField { get; set; }

    [Ignore]
    public bool SomeDbFieldAccessor
    {
        get { return (SomeDbField != null && SomeDbField == "Y"); }
        set { SomeDbField = value ? "Y" : "N"; }
    }
}

The [Ignore] attribute on the accessor field will tell ORMLite that the field should not be saved to / read from the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM