Home > .Net programming, NHibernate, Tools > NUnit with SQLite and .Net 4.0 Beta 2

NUnit with SQLite and .Net 4.0 Beta 2

SQLite and unit testing is a great combination for testing database operations without having to manage database files. You can simply create an in-memory database in your setup code and work with that instance. Perfect in combination with NHibernate, for example.

If you want to do this in the current .Net 4.0 beta you’re out of luck though, you’ll get an exception:

System.IO.FileLoadException: 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.

The solution is pointed out by Jomo Fisher. What you do is to include this snippet in the application config file:

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0"/>
</startup>

When unit testing assemblies that references System.Data.SQLite.DLL then you have to put that snippet in NUnit’s config file (C:\Program Files\NUnit 2.5.2\bin\net-2.0\nunit.exe.config).

If you combine this with the tip in my post NUnit with Visual Studio 2010 Beta 2, you should insert the following

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0"/>
  <requiredRuntime version="v4.0.20506" />
</startup>

plus

<loadFromRemoteSources enabled="true" />

under the runtime tag.

This works for me, hopefully it will for you as well.

/Emil

  1. November 24th, 2009 at 01:20 | #1

    Thanks for this Emil, it was very useful. System.Data.SQLite now happily working for me on 2010.

  2. claud
    February 15th, 2010 at 00:33 | #2

    Thanks a lot, i did a migration for my project from sqlce to sqlite and encountered this problem. Your solution worked.

  3. John
    February 18th, 2010 at 19:30 | #3

    Trying to figure out how to get this to work with the resharper test runner

  4. February 20th, 2010 at 13:23 | #4

    Me too :-)

    If you’re successful, please leave a note!

    /Emil

  5. mspspeak
    August 13th, 2010 at 23:27 | #5

    Thanks! I had the startup in my project config alongside my nunit file but didn’t think that the nunit.exe.config would need it.

    Awesome!
    @mspspeak

  6. Hossein
    October 17th, 2010 at 15:36 | #6

    Oh God! Thanks alot :) you saved me!!!

  7. Daniel
    March 16th, 2011 at 06:45 | #7

    A is a dll, B is also a dll, B will reference A.
    C is a exe, C will reference B.

    Debugging C, in B it alsways throws the same error at the place calling A as we talked.
    So where should the configuration be put, in C, A, or B?

  8. March 16th, 2011 at 21:35 | #8

    Well, as far as I know you can’t have configuration files for DLLs, unless you explicitly load them. So the answer is: C

  9. Daniel
    March 17th, 2011 at 01:51 | #9

    If C is also dll, which is that Extensibility project for office, so how can I do it?
    Actually, I don’t want to load the configuration.

  10. March 17th, 2011 at 08:23 | #10

    Ok, so you have an Office extension that needs access to a Sqlite database? I think you might be in trouble in that case.

    The correct place to insert the configuration in the post would be in the config file for Word/Excel/… but I don’t know if they even have config files. Even if they do, it feels very dirty to modify them.

    However you could try to find another Sqlite version that perhap might work better for you. This post was written almost 1.5 years ago and there have been newer versions since then, e.g. with 64 bit support. If you’re lucky you won’t need the config hacks in this post.

  11. Daniel
    March 17th, 2011 at 09:22 | #11

    Thank you very much.

    I’m sorry I did not describe cleary of my trouble. Actually, I don’t use Sqlite.

    All the dlls are developed by myself, any they are all upgrade from .net 2 to 4. The differences between them are that:
    C is extensibility project for outlook2003, and B is a middle dll used by C, and B just use the A. A is a dll that created from xml schema file(.xsd, the xsd is against .net2, when I upgraded it, just changed the target framework to 4), sure, A has been generated by the .net 4 tools, like xsd.exe, csc.exe.

  12. Daniel
    March 17th, 2011 at 09:25 | #12

    In B, there is a code like typeof(someclassname), the classname is difined in A, here, it always throw the error. I have tried adding configuration to each of them, but all failed.

    Hope above info can give you an overview of my question.

    Thanks again.

  13. March 18th, 2011 at 08:39 | #13

    Actually, I’m pretty confused about exactly what problem you’re having. :-) Do you mean that you’re getting the “Mixed mode assembly” exception?

    I’d say that your best bet would then be to make sure you generate/compile all your assemblies for .Net 2. I’m not sure if Office can work with .Net 4 components…

  14. Pali
    February 10th, 2012 at 13:24 | #14

    It works. Thanks!

  1. September 27th, 2010 at 16:51 | #1

Switch to our mobile site

Web Analytics