流利的nHibernate:如何坚持一个属性与公式映射? [英] fluent nHibernate: How to persist a property which is mapped with Formula?
问题描述
我试图将旧的DB文本字段映射到具有布尔值的类(只需要知道DB文本字段所具有的一个选项)。我可以使用Forumla从数据库中获取布尔值,但我似乎可以将它保存到数据库中。
我的类和当前的流畅映射因为它是:
public class Bulletin
{
public virtual int Id {get; set;}
public virtual bool RegularBulletin {get; set;}
}
public class BulletinMapping:ClassMap< Bulletin>
{
public BulletinMapping()
{
表(公告);
Id(x => x.Id,ID)。GeneratedBy.Identity();
$ b $ Map(x => x.RegularBulletin)
.Formula(case when EmailType ='BULLETIN_B'then 1 else null end)
.Nullable();
$ b 有没有人有关于如何坚持RegularBulletin
感谢
Saan
解决方案使用一个解决方法 - 创建一个支持字段受保护的虚拟字符串RegularBulletinString
,并使用您的布尔转换公式
public class Bulletin
{
public virtual int Id {get; set;}
保护的虚拟字符串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()
{
表(公告);
Id(x => x.Id,ID)。GeneratedBy.Identity();
Map(x => x.RegularBulletinString)
.Column(EmailType)
.Nullable();
IgnoreProperty(x => x.RegularBulletin);
}
}
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
解决方案 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);
}
}
这篇关于流利的nHibernate:如何坚持一个属性与公式映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!