[EnumAsInt] implementation in the class using the Enum type

Hi,

I use interface project to define my class to be identical between my server side code and client app.
I use different database layer access on each side.

Is it possible in my model class for ORMLite to put [EnumAsInt] instead then in the enum declaration ?

Like:

[Alias("valPercent")]
public int? Percent { get; set; }

[EnumAsInt] 
public UndigestFiber? UndigestFiber { get; set; }
public UndigestGrain? UndigestGrain { get; set; }

Thanks!
André

No both [Flags] and [EnumAsInt] attribute can only be attributed on an Enum Type which can also be added dynamically at runtime on Startup with:

typeof(MyDataModel).AddAttributes(new EnumAsIntAttribute());

The only other option to save it as a number value is change it to a numeric type, e.g. int?.

1 Like

At runtime is a nice alternative. Thanks!

Work great for Reading, but seem to always Write Zero when I try to use Db.Save()…

[Test]
public void Read_Audit_Record()
{
    var audit = Db.Single<RegularAudit>(a => a.Id == 2111);
    audit.BodyCondition = Db.Single<BodyCondition>(bc => bc.IdAudit == audit.UId && bc.Page == 2);
    audit.Locomotion = Db.Single<Locomotion>(loc => loc.IdAudit == audit.UId && loc.Page == 7);
    audit.ManureConsistency = Db.Single<ManureConsistency>(mc => mc.IdAudit == audit.UId && mc.Page == 4);
    audit.ManureDetails = Db.Select<ManureDetail>(md => md.IdAudit == audit.UId && md.Page == 9);
    audit.RumenFill = Db.Single<RumenFill>(rm => rm.IdAudit == audit.UId && rm.Page == 3);
    audit.RuminatingActivity = Db.Single<RuminatingActivity>(rma => rma.IdAudit == audit.UId && rma.Page == 1);
    audit.Cleanliness = Db.Single<Cleanliness>(c => c.IdAudit == audit.UId && c.Page == 5);
    audit.Environment = Db.Single<Regular.Environment>(c => c.IdAudit == audit.UId);

    var mdet = new ManureDetail();
    mdet.IdAudit = audit.UId;
    mdet.NumberOfGrain = REIDairy.Logic.Interface.Enumeration.Regular.UndigestGrain.GreaterThen5;  //1
    mdet.Consistency = REIDairy.Logic.Interface.Enumeration.Regular.ManureConsistency.Solid; //3
    mdet.UndigestFiber = REIDairy.Logic.Interface.Enumeration.Regular.UndigestFiber.Score2; //2

    Db.Save(mdet);

    Assert.That(audit.Id, Is.EqualTo(2111));
}

When I inspect with the debugger audit.ManureDetails each of my record have the ENUM resolved to my string.

When I SELECT the new inserted record my 3 enum fields are equal to 0. The enum value is <> then 0 for all 3. I use MySQL.

Db.Save does not seem to translate my ENUM to int.

Thanks!

Db.Insert() is working. I use .Core (5.4.0)

Please put together a stand-alone repro on GitHub I can run locally to repro the issue.

Interesting… I don’t have the issue with SQLite… Will tried my repro Monday when I have access to MySQL Database…

https://github.com/aleblanc70/ReproEnumAsInt

Thanks!

I’ve tried the project in both MySql and SQLite not seeing any issues in either.

Thanks mythz very appreciated.

I’m also able to use it when my column are string type even better :smile:
Don’t know what I did wrong but work very well

thanks!