开发者

fluent nHibernate: How to persist a property which is mapped with Formula?

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, "I开发者_开发知识库D").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


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

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜