1
votes

I am dealing with a legacy database, and we have a field which doesn't make sense anymore, but I would rather not change the DB schema.

I'm trying to map an old DB text field into a class with a boolean (only need to know about one option that the DB text field has). I can get the boolean value out of the DB using Forumla, but I can seem to get it to save any updates back into the DB.

My class and current fluent mapping for it is:

public class Bulletin
{
    public virtual int Id { get; set;}
    public virtual bool RegularBulletin { get; set;}
}

public class BulletinMapping : ClassMap<Bulletin>
{
    public BulletinMapping()
    {
        Table("Bulletins");
        Id(x => x.Id, "ID").GeneratedBy.Identity();

        Map(x => x.RegularBulletin)
            .Formula("case when EmailType = 'BULLETIN_B' then 1 else null end")
            .Nullable();
    }
}

Does anyone have any ideas about how to persist the RegularBulletin field?

Thanks Saan

1
So what does it insert in that table column when saving? Or Can you not save any data and get an exception?VoodooChild
Currently it doesn't save anything and doesn't error. Its meant to save BULLETIN_B or nullSaan
Are you wanting the EmailType value to be updated when you change the value of RegularBulletin (ie RegularBulletin was false, you set it to true and saved and are wanting EmailType to be updated with a value of "BULLETIN_B")?docmanhattan

1 Answers

3
votes

I would use a workaround for this- create a backing field protected virtual string RegularBulletinString and use your boolean conversion formula on it.

public class Bulletin
{
    public virtual int Id { get; set;}
    protected virtual string RegularBulletinString { get; set;}
    public virtual bool RegularBulletin 
    { 
       get
       {
          return RegularBulletinString == "BULLETIN_B";
       } 
       set
       {
          RegularBulletinString = value? "BULLETIN_B" : null;
       }
    }
}

public class BulletinMapping : ClassMap<Bulletin>
{
    public BulletinMapping()
    {
        Table("Bulletins");
        Id(x => x.Id, "ID").GeneratedBy.Identity();

        Map(x => x.RegularBulletinString)
            .Column("EmailType")
            .Nullable();
        IgnoreProperty(x=> x.RegularBulletin); 

    }
}