I get Exception when using Sqlite with Nhibernate for integration tests: TypeLoadException":generic arguments[0], "system.nullable {system.datetimeOffset} on System.Nullable[T] violates the constraint of type parameter T
I am using below to configure sqllite:
public InMemoryDatabaseTest()
{
SessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.ProxyFactoryFactory(typeof(ProxyFactoryFactory))
.Mappings(m => m.FluentMappings
.Add(typeof(UserMapping)
.Conventions.Add(new NormalizedDateTimeUserTypeConversion(),new NormalizedDateTimeUserTypeConversion1()))
.ExposeConfiguration(x => configuration = x)
.BuildSessionFactory();
session = SessionFactory.OpenSession();
SchemaExport export = new SchemaExport(configuration);
export.Execute(true, true, false, session.Connection, null);
}
public void Dispose()
{
session.Dispose();
}
Here is the implementation of NormalizedDateTimeUserType:
public class NormalizedDateTimeUserType : IUserType
{
private readonly TimeZoneInfo databaseTimeZone = TimeZoneInfo.Local;
// Other standard interface implementations omitted ...
public Type ReturnedType
{
get { return typeof(DateTimeOffset); }
}
public SqlType[] SqlTypes
{
get { return new[] { new SqlType(DbType.DateTime) }; }
}
public object NullSafeGet(IDataReader dr, string[] names, object owner)
{
object r = dr[names[0]];
if (r == DBNull.Value)
{
return null;
}
DateTime storedTime = (DateTime)r;
return new DateTimeOffset(storedTime, this.databaseTimeZone.BaseUtcOffset);
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if (value == null)
{
NHibernateUtil.DateTime.NullSafeSet(cmd, null, index);
}
else
{
DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
DateTime paramVal = dateTimeOffset.ToOffset(this.databaseTimeZone.BaseUtcOffset).DateTime;
IDataParameter parameter = (IDataParameter)cmd.Parameters[index];
parameter.Value = paramVal;
}
}
}
Implementation of NormalizedDateTimeUserType1:
public class NormalizedDateTimeUserType1 :
NormalizedDateTimeUserType
{
public override Type ReturnedType
{
get { return typeof(DateTimeOffset?); }
}
}