Mixing NHibernate, Fluent NHibernate, and SQLite on .NET 4.0 RC will lead to some major headaches. There’s an easy fix, but it doesn’t work if you are using Resharper’s test runner. If you’re using my personal favorite test runner, TestDriven.NET though, the fix is actually quite easy.
We decided to use .NET 4.0 for the new super secret social networking site we’re working on, and since we’re starting out with a relational DB under the hood (that we fully plan to swap out for a NoSQL alternative later), we’re using NHibernate. I hate NHibernate’s XML maps, so we’re also using FluentNHibernate. When I’m using NHibernate, I like to use SQLite’s in-memory database for my unit tests, so now we’re looking at making three pieces of OSS play nice together on a not-quite-finished version of the .NET Framework. Oh, and I’m also using Resharper 5.0 EAP. 🙂
Getting things started is easy enough. I grabbed the latest production release of NHibernate along with a compatible version of Fluent NHibernate. I also grabbed the latest SQLite .NET assembly. After adding references to the required bits, here’s my NHibernate bootstrapper:
public static class NHibernateBootstrapper { private static Configuration _currentConfig; private static FluentConfiguration ConfigureMappings() { return Fluently.Configure() .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())) .ExposeConfiguration(c => _currentConfig = c); } public static ISessionFactory CreateSessionFactoryForTests() { return ConfigureMappings() .Database(SQLiteConfiguration.Standard.ShowSql().InMemory()) .BuildSessionFactory(); } public static ISessionFactory CreateSessionFactoryForProduction() { return ConfigureMappings() .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("RageFeedData"))) .BuildSessionFactory(); } public static void CreateSchema(ISession session) { var exporter = new SchemaExport(_currentConfig); exporter.Execute(true, true, false, session.Connection, null); } public static void UpdateSchema() { var updater = new SchemaUpdate(_currentConfig); updater.Execute(true, true); } }
And here’s my unit tests:
public abstract class InMemoryDatabaseFixture { public ISession Session { get; private set; } [SetUp] public virtual void Prepare() { var factory = NHibernateBootstrapper.CreateSessionFactoryForTests(); Session = factory.OpenSession(); NHibernateBootstrapper.CreateSchema(Session); } [TearDown] public virtual void Cleanup() { Session.Dispose(); } } [TestFixture] public class RageletPersistanceTests : InMemoryDatabaseFixture { [Test] public void CanCorrectlyPersistRagelet() { new PersistenceSpecification<Ragelet>(Session) .CheckProperty(r => r.Content, "Test content") .VerifyTheMappings(); } }
Simple enough, right?
Sadly, this fails gloriously when run with ReSharper’s test runner:
Mixed mode assembly is built against version ‘v2.0.50727’ of the runtime
and cannot be loaded in the 4.0 runtime without additional configuration information.
I found a solution, but it doesn’t work with Resharper 5.0 EAP. I’m thinking that Resharper isn’t using the app.config file, but perhaps it’s something else. Fortunately, running the tests with TestDriven.NET (my favorite test runner) worked perfectly.
Unfortunately, I wasted the better part of two weeks of “free time” trying to get things recompiled so that they would work without the app.config entry. There are several breaking changes in .NET 4.0 apparently, and nothing that I tried worked. Hopefully this isn’t a sign of pains to come.
Wow. just searched google and found exactly what i was looking for great post.
Morning i just came across your site from Yahoo and have been reading some of your entries and just wondering why you selected a Blog Engine blog dont you find it impossible to do anything with?
Very nice, site I would say the information I found helpful. I’ve saved it and definately will have a look at once again afterward. Thanks a lot.
Very nice, page The data I recently found productive. We’ve made note of it and definately will have a look at once more shortly after. Appreciate it.
It’s nice site That information I came across important. We have made note of it and will eventually stop by for a second time subsequently. Best wishes.
Would be interested in your project… What is a Ragelet?
🙂 I’ll be posting more about what a "Ragelet" is soon. The project is still in early development, and I don’t want to spill the beans until we’re closer to beta.
Hey admin, your website’s design is awesome and i like it. Your articles are fantastic. Please continue this awesome work. Greets!. play tetris game
It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks
What a coincidence!! i was just searching for wordpress comment page plugins and came across your site. Nice list.
Ended up here trying to find some thing totally else, but ended up to be an interesting read anyway, so I guess sometimes it’s ok that you can’t use a search engine properly.
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
Thanks for piecing this together – this is a great article for those of us with our heads buried in the keyboard all day.
cool, I got it.
Me & my fellow classmates use your blogs as our reference materials. We look out for more interesting posts from your end about the same topic . Even the future updates about this topic would be of great help.
Superbly written post, if only all website owners offered the same quality information as you, the internet would be a much better place. Please keep it up! Cheers.
Well not quite what I was searching for nonetheless it was some good reading anyway, guess its a ok that I wasn’t capable of doing a search properly or I wouldn’t ended up here.
One of the things I really like about website blogs is that often they trigger a thought in my brain. Once that happens, I feel as I need to write a comment with the hope it may be pleasant to some people. Since there are numerous blogs and forums with unique points of view, they encourage your perception. It is at these moments when you have exceptional insignt many others might not have had, including the blogger him/herself. I find myself coming back to your website because you have some impressive insights and you are actually at this a very long time, that’s very inspiring and tells me you know a lot. Keep triggering thoughts in other people!
Stumbled into this site by chance but I’m sure glad I clicked on that link. You definitely answered all the questions I’ve been dying to answer for some time now. Will definitely come back for more of this. Thank you so much
Hello. Fantastic job, if I wasn’t so busy with my school work I read your full site. Thanks!
Good read! You need to get some social bookmarking buttons on your posts so that we can share stuff like this with the click of a button.
Sorry if my english is not good. i just desire to say i like this article a lot. many thanks
This is a nice website. Are you using WordPress as your platform? I ask that because your website looks a little different…
I’m regularly bouncing all over the net most of the time which means I possess a tendency to browse a ton, which unfortunately is not typically a good thing as most of the web pages I find are composed of pointless waste copied from several other web pages a trillion times, on the other hand I have to give you credit this website is certainly decent and also contains a lot of authentic substance, for that reason thank you for breaking the phenomena of exactly duplicating other peoples’ blogs and forums… Nice work!
Can’t think of better ways to write this post , did a great job with this topic.
Hey bro, nice blog! I am signing up for the RSS feed.
Hi,Folks. I come here form yahoo,I consider this version is nice ,.thanks you for sharing.
Can I use scraped content or article directory articles? No, we expect our publishers to write original content. We respect the copyright of all publishers and will not allow publishers who choose to use other people’s copyrighted material. http://www.balmel.com.
Why place a link in our Website Directory? Our Website Directory is poised to become the largest directory. They allow for 100% free URL linking. Our directory is search engine friendly. We have strict guidelines to make sure that sites added to our directory are quality sites. We use the latest in technology to circumvent spam and all other kind of abuse. http://www.melbal.com.
Without doubt agree with what you said. Your explanation was undoubtedly the simplest to understand. I tell you, I often get annoyed when folks explain things that they plainly don’t know about. You managed to hit the nail right on the head and also explained out everything clearly. Maybe, others can take a cue. Will likely be back for additional. Thanks
Excellent post! I am forwarding this to my friends. I just realized that your platform is not WordPress. I am looking to change my WordPress blog to a different platform. Do you have any suggestions?