On Wednesday, September 14, 2011 3:44:07 PM UTC-4, Tim Scott wrote:
>
> I didn't notice that CharBooleanType is abstract. There's TrueFalseType and
> YesNo type, neither of which are what you need. Two ideas:
>
> One of the answers to this post says you can do it with query substitutions
> alone. You might try that. You probably want to set query substitutions in
> any case.
>
> http://stackoverflow.com/questions/7404991/fluentnhibernate-how-to-map-database-char-to-c-bool
>
> Otherwise, you can do it for sure with a custom IUserType:
>
> http://lostechies.com/rayhouston/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype/
>
> Copy the sample code and just change the NullSafeGet and NullSafeSet to the
> behavior you want. Then map your custom type in FNH (instead of
> CharBooleanType).
>
I followed the example at lostechies.com, and this is what I have for
NullSafeGet and NullSafeSet:
public object NullSafeGet(Idr dr, string[] names, object owner) {
var obj = NHibernateUtil.String.NullSafeGet(dr, names[0]);
if (String.IsNullOrWhiteSpace(Convert.ToString(obj))) {
return null;
}
if (Convert.ToString(obj) != "1" && Convert.ToString(obj) !=
"0") {
throw new Exception("Expected data to be '1' or '0', but was
" + obj);
}
return Convert.ToString(obj) == "1";
}
public void NullSafeSet(IDbCommand dbCommand, object value, int
index) {
if (value == null) {
((IDataParameter)dbCommand.Parameters[index]).Value =
DBNull.Value;
}
else {
var yes = (bool)value;
((IDataParameter)dbCommand.Parameters[index]).Value = yes ?
"1" : "0";
}
}
Then in my Mappings.cs, I have
Map(x => x.IsInactive).Column("inactive").CustomType<CustomBooleanType>();
No build error, but at run time, I get this:
System.InvalidCastException : Unable to cast object of type
'System.Boolean' to type 'MyProject.Mappings.CustomBooleanType'.
So, what am I missing? Thanks.
> On Wednesday, September 14, 2011 at 1:43 PM, Influently NHiberater wrote:
>
> >
> > On Wednesday, September 14, 2011 2:26:13 PM UTC-4, Influently NHiberater
> wrote:
> > >
> > > On Wednesday, September 14, 2011 1:55:39 PM UTC-4, Tim Scott wrote:
> > > > Try this:
> > > > Map(x=>x.IsActive).Column("is_active").CustomType<CharBooleanType>()
> > > > --
> > > > Tim Scott
> > > > Lunaverse Software
> >
> >
> > OK, just found that CharBooleanType is under NHibernate.Type. So, I tried
> like what you said:
> >
> > Map(x =>
> x.IsActive).Column("is_active").CustomType<NHibernate.Type.CharBooleanType>();
> >
> > No build error, but at compile time, I got this:
> >
> > An invalid or incomplete configuration was used while creating a
> SessionFactory. Check PotentialReasons collection, and InnerException for
> more detail.
> >
> > ----> FluentNHibernate.Cfg.FluentConfigurationException : An invalid or
> incomplete configuration was used while creating a SessionFactory. Check
> PotentialReasons collection, and InnerException for more detail.
> >
> > ----> NHibernate.MappingException : Could not compile the mapping
> document: (XmlDocument)
> > ----> NHibernate.MappingException : Could not instantiate IType
> CharBooleanType: System.MissingMethodException: Cannot create an abstract
> class.
> >
> >
> >
> >
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups "Fluent NHibernate" group.
> > To view this discussion on the web visit
> https://groups.google.com/d/msg/fluent-nhibernate/-/f9yCrRQed_YJ.
> > To post to this group, send email to [email protected](mailto:
> [email protected]).
> > To unsubscribe from this group, send email to
> [email protected] (mailto:
> [email protected]).
> > For more options, visit this group at
> http://groups.google.com/group/fluent-nhibernate?hl=en.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Fluent NHibernate" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/fluent-nhibernate/-/q9Nwc_PAsEAJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/fluent-nhibernate?hl=en.