Reputation: 1266
Ok, here's the deal.
I have a webApi project in ASP.NET Core, that references project int .NET Framework with nHibernate.
I want to design some integration tests but I can't figure out the way to hook up SQLite driver.
I've end up with something like this:
Configuration = new NHibernate.Cfg.Configuration();
IDictionary<string, string> properties = new Dictionary<string, string>
{
// Setup database connection
{NHibernate.Cfg.Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider"},
{NHibernate.Cfg.Environment.Dialect, "NHibernate.Dialect.SQLiteDialect"},
{NHibernate.Cfg.Environment.ConnectionDriver, "MilestoneTG.NHibernate.Driver.Sqlite.Microsoft.MicrosoftSqliteDriver, MilestoneTG.NHibernate.Driver.Sqlite.Microsoft"},
{NHibernate.Cfg.Environment.ConnectionString, "Data Source=:memory:;New=True;"},
{NHibernate.Cfg.Environment.Isolation, "ReadCommitted"},
{NHibernate.Cfg.Environment.CurrentSessionContextClass, "call"}
};
Configuration.AddProperties(properties);
Configuration.AddAssembly(typeof(NHibernateConfiguration).Assembly);
var export = new SchemaExport(Configuration);
export.Execute(true, true, false);
SessionFactory = Configuration.BuildSessionFactory();
SchemaExport.Execute() throws an Exception telling me that SQLite dialect cannot deal with DbType.DateTimeOffset.
Natural way out of this would be using the custom convention but I cannot do that because it's asp.net core :/
All mappings are stored in hbm.xml files. I cannot modify nHibernate configuration. I have to stick up with asp.net core.
Additionally, only one class uses DateTimeOffset and it's rather expendable, but I can't figure out the way to import all other classes except this one.
Any ideas how to solve this?
Upvotes: 0
Views: 465
Reputation: 1266
I've made it to work. Not in the elegant way but still.
As I mentioned before there was one class that was problematic because of DateTimeOffset property.
One option was to get rid of this class from model.
I couldn't use AddClass() method to add all classes but this one because naming convention wasn't preserved. So I figured a workaround:
I've iterated through all classes (except the problematic one) and built "*.hbm.xml" file names for each of them and then I've used AddResource() method to add each file to configuration.
foreach (var type in assembly.GetTypes().Where(t => typeof(BaseModel).IsAssignableFrom(t) && t.Name != nameof(ProblematicType) && !t.IsAbstract))
{
var mappingName = string.Join(".", type.Namespace.Replace("Model", "DataMapping"), type.Name) + ".hbm.xml";
configuration.AddResource(mappingName, assembly);
}
It would look something like that (I've omitted project specific lines).
I know it's probably not the best way to do it and of course it doesn't fix the root problem but it works fine.
Upvotes: 1