Tigraine
Daniel Hoelbling talks about .NET

Fluent NHibernate gotchas when testing with an in memory database.

May 29th, 2009 . by Daniel Hölbling

What I love most about programmatic configuration is that it’s close to the test.
While we were carrying dozens of XML files around for testing before, now with DSL based configuration everywhere the configuration is usually pretty near to the test fixture, instead of residing in some arbitrary XML that only insiders can associate with the test.

The standard sample for using SqlLite and Fluent NHibernate usually looks like this:

return
    Fluently
        .Configure()
        .Database(SQLiteConfiguration.Standard.UsingFile("mydb.db3").ShowSql())
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionFactory>())
        .ExposeConfiguration(SaveSchema)
        .BuildSessionFactory();

Where SaveSchema is a method that does a database rebuild.

Now, Fluent Nhibernate has in-memory databases built into the API. Just remove the UsingFile directive and you replace it with:

.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())

Charming isn’t it? Now the only problem is that you won’t be able to do anything with that DB since there is no schema present.

The in-memory database exists per session, so once you close the ISession the db is gone. Since the schema export from most samples operates in it’s own ISession the subsequent queries will still hit a blank database, and you’ll get an error stating there is no such table.

So my SessionFactory implementation had to change, since I needed to keep the configuration around for doing the schema export:

public class SessionFactory
{
    public static ISessionFactory CreateSessionFactory()
    {
        return
            Fluently
                .Configure()
                .Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionFactory>())
                .ExposeConfiguration((c) =>  SavedConfig = c)
                .BuildSessionFactory();
    }

    private static Configuration SavedConfig;

    public static void BuildSchema(ISession session)
    {
        var export = new SchemaExport(SavedConfig);
        export.Execute(true, true, false, false, session.Connection, null);
    }
}

And my tests then use a another factory method to construct the ISession object:

public static ISession CreateSession()
{
    var factory = SessionFactory.CreateSessionFactory();
    var session = factory.OpenSession();
    SessionFactory.BuildSchema(session);

    return session;
}

Hope this helps, quite an annoying problem and imo a far from perfect solution. Someone on the FNH mailing list suggested looking at the OneToManyIntegrationTester class but I couldn’t really extract any terribly useful information from there.


View Comments to “Fluent NHibernate gotchas when testing with an in memory database.”

  1. comment number 1 by: James Allen

    Thank you so much for this post, you have finally ended my pain trying to get this to work! What I hadn’t worked out was that you should save the configuration to use later in the schema export. Seems so simple now…


  2. [...] the schema into the in-memory database. For the latter I’m grateful to Daniel Hölbling for his post describing how to do the latter using Fluent. Here’s the finished [...]


  3. [...] the title sounds familiar to you, it’s intentional. After having to deal with this in pure NHibernate it came around to also bite me with [...]


  4. [...] Ayende’s blog post, Krzysztof Kozmic’s recent post on Devlio.us, Daniel Hoebling’s blog post, Ayende’s NHProfiler [...]

  5. comment number 5 by: RT

    Thanks for the post.

    I was wondering whether it would be helpful to create the session factory once per test fixture rather than once per session, which will be created once per test within a fixture.

  6. comment number 6 by: Daniel Hölbling

    Actually, I am a strong believer in tests not interfering with each other.
    By having all of my tests in one test-class share the same database instance, I am giving it shared state.
    With a shared state among my tests I might run into issues of tests depending on order of execution to pass, thus making them pretty much worthless.

    The alternative is to do a manual cleanup after each fixture to return the shared session back to a known state so it can be used for the next test. And imo that's not really worth the effort

    greetings Daniel

  7. comment number 7 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  8. comment number 8 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  9. comment number 9 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  10. comment number 10 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  11. comment number 11 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  12. comment number 12 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  13. comment number 13 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  14. comment number 14 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  15. comment number 15 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  16. comment number 16 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  17. comment number 17 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  18. comment number 18 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  19. comment number 19 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  20. comment number 20 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  21. comment number 21 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  22. comment number 22 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  23. comment number 23 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  24. comment number 24 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  25. comment number 25 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  26. comment number 26 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  27. comment number 27 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  28. comment number 28 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  29. comment number 29 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  30. comment number 30 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  31. comment number 31 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  32. comment number 32 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  33. comment number 33 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  34. comment number 34 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  35. comment number 35 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  36. comment number 36 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  37. comment number 37 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  38. comment number 38 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  39. comment number 39 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  40. comment number 40 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  41. comment number 41 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  42. comment number 42 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  43. comment number 43 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  44. comment number 44 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  45. comment number 45 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  46. comment number 46 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  47. comment number 47 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  48. comment number 48 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  49. comment number 49 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  50. comment number 50 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  51. comment number 51 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  52. comment number 52 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  53. comment number 53 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  54. comment number 54 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  55. comment number 55 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  56. comment number 56 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  57. comment number 57 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  58. comment number 58 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  59. comment number 59 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  60. comment number 60 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  61. comment number 61 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  62. comment number 62 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  63. comment number 63 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  64. comment number 64 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  65. comment number 65 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  66. comment number 66 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  67. comment number 67 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  68. comment number 68 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  69. comment number 69 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  70. comment number 70 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  71. comment number 71 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  72. comment number 72 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  73. comment number 73 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  74. comment number 74 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  75. comment number 75 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  76. comment number 76 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  77. comment number 77 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  78. comment number 78 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  79. comment number 79 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  80. comment number 80 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  81. comment number 81 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  82. comment number 82 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  83. comment number 83 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  84. comment number 84 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  85. comment number 85 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  86. comment number 86 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  87. comment number 87 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  88. comment number 88 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  89. comment number 89 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  90. comment number 90 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  91. comment number 91 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article

  92. comment number 92 by: Daniel Hu00f6lbling

    Glad it helped..

  93. comment number 93 by: Daniel Hu00f6lbling

    Glad it helped..

  94. comment number 94 by: Daniel Hu00f6lbling

    Glad it helped..

  95. comment number 95 by: Daniel Hu00f6lbling

    Glad it helped..

  96. comment number 96 by: Daniel Hu00f6lbling

    Glad it helped..

  97. comment number 97 by: Daniel Hu00f6lbling

    Glad it helped..

  98. comment number 98 by: Daniel Hu00f6lbling

    Glad it helped..

  99. comment number 99 by: Daniel Hu00f6lbling

    Glad it helped..

  100. comment number 100 by: Daniel Hu00f6lbling

    Glad it helped..

  101. comment number 101 by: Daniel Hu00f6lbling

    Glad it helped..

  102. comment number 102 by: Daniel Hu00f6lbling

    Glad it helped..

  103. comment number 103 by: Daniel Hu00f6lbling

    Glad it helped..

  104. comment number 104 by: Daniel Hu00f6lbling

    Glad it helped..

  105. comment number 105 by: Daniel Hu00f6lbling

    Glad it helped..

  106. comment number 106 by: Daniel Hu00f6lbling

    Glad it helped..

  107. comment number 107 by: Daniel Hu00f6lbling

    Glad it helped..

  108. comment number 108 by: Daniel Hu00f6lbling

    Glad it helped..

  109. comment number 109 by: Daniel Hu00f6lbling

    Glad it helped..

  110. comment number 110 by: Daniel Hu00f6lbling

    Glad it helped..

  111. comment number 111 by: Daniel Hu00f6lbling

    Glad it helped..

  112. comment number 112 by: Daniel Hu00f6lbling

    Glad it helped..

  113. comment number 113 by: Daniel Hu00f6lbling

    Glad it helped..

  114. comment number 114 by: Daniel Hu00f6lbling

    Glad it helped..

  115. comment number 115 by: Daniel Hu00f6lbling

    Glad it helped..

  116. comment number 116 by: Daniel Hu00f6lbling

    Glad it helped..

  117. comment number 117 by: Daniel Hu00f6lbling

    Glad it helped..

  118. comment number 118 by: Daniel Hu00f6lbling

    Glad it helped..

  119. comment number 119 by: Daniel Hu00f6lbling

    Glad it helped..

  120. comment number 120 by: Daniel Hu00f6lbling

    Glad it helped..

  121. comment number 121 by: Daniel Hu00f6lbling

    Glad it helped..

  122. comment number 122 by: Daniel Hu00f6lbling

    Glad it helped..

  123. comment number 123 by: Daniel Hu00f6lbling

    Glad it helped..

  124. comment number 124 by: Daniel Hu00f6lbling

    Glad it helped..

  125. comment number 125 by: Daniel Hu00f6lbling

    Glad it helped..

  126. comment number 126 by: Daniel Hu00f6lbling

    Glad it helped..

  127. comment number 127 by: Daniel Hu00f6lbling

    Glad it helped..

  128. comment number 128 by: Daniel Hu00f6lbling

    Glad it helped..

  129. comment number 129 by: Daniel Hu00f6lbling

    Glad it helped..

  130. comment number 130 by: Daniel Hu00f6lbling

    Glad it helped..

  131. comment number 131 by: Daniel Hu00f6lbling

    Glad it helped..

  132. comment number 132 by: Daniel Hu00f6lbling

    Glad it helped..

  133. comment number 133 by: Daniel Hu00f6lbling

    Glad it helped..

  134. comment number 134 by: Daniel Hu00f6lbling

    Glad it helped..

  135. comment number 135 by: Daniel Hu00f6lbling

    Glad it helped..

  136. comment number 136 by: Daniel Hu00f6lbling

    Glad it helped..

  137. comment number 137 by: Daniel Hu00f6lbling

    Glad it helped..

  138. comment number 138 by: Daniel Hu00f6lbling

    Glad it helped..

  139. comment number 139 by: Daniel Hu00f6lbling

    Glad it helped..

  140. comment number 140 by: Daniel Hu00f6lbling

    Glad it helped..

  141. comment number 141 by: Daniel Hu00f6lbling

    Glad it helped..

  142. comment number 142 by: Daniel Hu00f6lbling

    Glad it helped..

  143. comment number 143 by: Daniel Hu00f6lbling

    Glad it helped..

  144. comment number 144 by: Daniel Hu00f6lbling

    Glad it helped..

  145. comment number 145 by: Daniel Hu00f6lbling

    Glad it helped..

  146. comment number 146 by: Daniel Hu00f6lbling

    Glad it helped..

  147. comment number 147 by: Daniel Hu00f6lbling

    Glad it helped..

  148. comment number 148 by: Daniel Hu00f6lbling

    Glad it helped..

  149. comment number 149 by: Daniel Hu00f6lbling

    Glad it helped..

  150. comment number 150 by: Daniel Hu00f6lbling

    Glad it helped..

  151. comment number 151 by: Daniel Hu00f6lbling

    Glad it helped..

  152. comment number 152 by: Daniel Hu00f6lbling

    Glad it helped..

  153. comment number 153 by: Daniel Hu00f6lbling

    Glad it helped..

  154. comment number 154 by: Daniel Hu00f6lbling

    Glad it helped..

  155. comment number 155 by: Daniel Hu00f6lbling

    Glad it helped..

  156. comment number 156 by: Daniel Hu00f6lbling

    Glad it helped..

  157. comment number 157 by: Daniel Hu00f6lbling

    Glad it helped..

  158. comment number 158 by: Daniel Hu00f6lbling

    Glad it helped..

  159. comment number 159 by: Daniel Hu00f6lbling

    Glad it helped..

  160. comment number 160 by: Daniel Hu00f6lbling

    Glad it helped..

  161. comment number 161 by: Daniel Hu00f6lbling

    Glad it helped..

  162. comment number 162 by: Daniel Hu00f6lbling

    Glad it helped..

  163. comment number 163 by: Daniel Hu00f6lbling

    Glad it helped..

  164. comment number 164 by: Daniel Hu00f6lbling

    Glad it helped..

  165. comment number 165 by: Daniel Hu00f6lbling

    Glad it helped..

  166. comment number 166 by: Daniel Hu00f6lbling

    Glad it helped..

  167. comment number 167 by: Daniel Hu00f6lbling

    Glad it helped..

  168. comment number 168 by: Daniel Hu00f6lbling

    Glad it helped..

  169. comment number 169 by: Daniel Hu00f6lbling

    Glad it helped..

  170. comment number 170 by: Daniel Hu00f6lbling

    Glad it helped..

  171. comment number 171 by: Daniel Hu00f6lbling

    Glad it helped..

  172. comment number 172 by: Daniel Hu00f6lbling

    Glad it helped..

  173. comment number 173 by: Daniel Hu00f6lbling

    Glad it helped..

  174. comment number 174 by: Daniel Hu00f6lbling

    Glad it helped..

  175. comment number 175 by: Daniel Hu00f6lbling

    Glad it helped..

  176. comment number 176 by: Daniel Hu00f6lbling

    Glad it helped..

  177. comment number 177 by: m4bwav

    You saved many hairs in my head from the threat of SqlLite induced stress.

    Great Article

  178. comment number 178 by: Daniel Hölbling

    Glad it helped..

Leave a Reply

Name

Mail (never published)

Website

blog comments powered by Disqus