Search code examples
.netnhibernatefluentsystem.data

How can I get fluent nhibernate working in .net 4


I am using all the dlls from the standard fluent nhibernate download, as well as the standard sqlite dll.

It only works if I switch target framework to 3.5 -- in which case the reference for system.data is switched to version 2. -- so this dll is probably the reason.

I am suprised that fluent nhibernate doesn't seem to work straight away.

If someone could tell me how to get a .net 4 console app to use the .net 3.5 system.data dll (version 2.) then that might fix it. Or if there is a secret fluent nhibernate download somewhere that works...

Edit: OK here's some error message.......


Edit: SOLVED! With this app.config file:

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true"><supportedRuntime version="v4.0"/>
  </startup>
</configuration>

Unhandled Exception: FluentNHibernate.Cfg.FluentConfigurationException: An inval
id or incomplete configuration was used while creating a SessionFactory. Check P
otentialReasons collection, and InnerException for more detail.

 ---> FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplet
e configuration was used while creating a SessionFactory. Check PotentialReasons
 collection, and InnerException for more detail.

 ---> NHibernate.HibernateException: Could not create the driver from NHibernate
.Driver.SQLite20Driver, NHibernate, Version=3.1.0.4000, Culture=neutral, PublicK
eyToken=aa95f207798dfdb4. ---> System.Reflection.TargetInvocationException: Exce
ption has been thrown by the target of an invocation. ---> System.IO.FileLoadExc
eption: 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 inform
ation.
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError,
Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, Bool
ean loadTypeFromPartialName, ObjectHandleOnStack type)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError,
Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean l
oadTypeFromPartialName)
   at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean
ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
   at System.Type.GetType(String typeName)
   at System.Data.Common.DbProviderFactories.GetFactory(DataRow providerRow)
   at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariant
Name)
   at NHibernate.Driver.ReflectionBasedDriver..ctor(String providerInvariantName
, String driverAssemblyName, String connectionTypeName, String commandTypeName)
in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Driver\ReflectionBasedDriver.cs:lin
e 49
   at NHibernate.Driver.SQLite20Driver..ctor() in d:\CSharp\NH\NH\nhibernate\src
\NHibernate\Driver\SQLite20Driver.cs:line 28
   --- End of inner exception stack trace ---
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOn
ly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Bo
olean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipChec
kThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean s
kipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at NHibernate.Bytecode.ActivatorObjectsFactory.CreateInstance(Type type) in d
:\CSharp\NH\NH\nhibernate\src\NHibernate\Bytecode\ActivatorObjectsFactory.cs:lin
e 9
   at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 set
tings) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Connection\ConnectionProvide
r.cs:line 98
   --- End of inner exception stack trace ---
   at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 set
tings) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Connection\ConnectionProvide
r.cs:line 113
   at NHibernate.Connection.ConnectionProvider.Configure(IDictionary`2 settings)
 in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Connection\ConnectionProvider.cs:l
ine 64
   at NHibernate.Connection.ConnectionProviderFactory.NewConnectionProvider(IDic
tionary`2 settings) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Connection\Conn
ectionProviderFactory.cs:line 50
   at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolea
n export, Boolean justDrop) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Tool\hb
m2ddl\SchemaExport.cs:line 330
   at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Boolean script, Boolean expor
t, Boolean justDrop) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Tool\hbm2ddl\S
chemaExport.cs:line 290
   at NHibernate.Tool.hbm2ddl.SchemaExport.Create(Boolean script, Boolean export
) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Tool\hbm2ddl\SchemaExport.cs:line
 105
   at FluentNHibernateTest4.Program.BuildSchema(Configuration config) in C:\User
s\anna\Documents\Visual Studio 2010\Projects\TestFluentNHibernate\FluentNHiberna
teTest4\Program.cs:line 97
   at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in d:\Builds
\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 254
   --- End of inner exception stack trace ---
   at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in d:\Builds
\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 261
   at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in d:\Build
s\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 227
   --- End of inner exception stack trace ---

Solution

  • Probably the easiest way to reference Fluent nHibernate in a Visual Studio 2010 project is to install the NuGet Extension (http://nuget.codeplex.com/) and use that to add Fluent nHibernate and it's dependencies to your project.

    SQLite can also be added to your project using this method (there are currently two packages available SQLite (32bit only) and SQLitex64 (32bit+64bit). The SQLite NuGet package will also add the additional config required to resolve the exception you're receiving:

    NHibernate.HibernateException: Could not create the driver from NHibernate
    .Driver.SQLite20Driver, NHibernate, Version=3.1.0.4000, Culture=neutral, PublicK
    eyToken=aa95f207798dfdb4. ---> System.Reflection.TargetInvocationException: Exce
    ption has been thrown by the target of an invocation. ---> System.IO.FileLoadExc
    eption: 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 inform
    ation.
    

    This exception is also dealt with in this question: What 'additional configuration' is necessary to reference a .NET 2.0 mixed mode assembly in a .NET 4.0 project?