First Steps with NHibernate, Fluent, Oracle, C#, and Visual Studio 2010 RC

A few days into a new project: C#, Oracle, ASP.Net, .Net 4, VS 2010. After creating a (very!) basic data model with a couple of tables, the first task was to check whether the intended tools (C# 4, NHibernate+Fluent, ODP.Net, Oracle 11g) can be persuaded to co-operate.

Versions of tools and libraries used:

After I had defined my classes and maps as described in Fluent’s Getting Started wiki page, the first major hurdle was to get the session factory builder to execute without exception:

public static ISessionFactory Configure()
{
  var c = Fluently.Configure();
  c.Database(OracleDataClientConfiguration.Oracle10
    .ConnectionString(x => x.FromConnectionStringWithKey("default"))
    .DefaultSchema("cpa"));
  c.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ConfigureDAL>());
  return c.BuildSessionFactory();
}

Creating an NHibernate session factory with Fluent, connecting to an Oracle database via ODP.Net.

Fluent provides 3 classes for Oracle access in the FluentNHibernate.Cfg.Db namespace:

  • OracleClientConfiguration for the MS Oracle Client (System.Data.OracleClient, deprecated)
  • OracleConfiguration, obsolete and replaced by:
  • OracleDataClientConfiguration for use with ODP

The connection string is stored in app.config under the name “default”:

<connectionStrings>
  <add name="default"
    connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)
      (HOST=orasrv)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)
      (SERVICE_NAME=orcl)));User Id=cpa;Password=the_password;" />
</connectionStrings>

Trying to execute the BuildSessionFactory() method raised the first exception:

The element ‘class’ in namespace ‘urn:nhibernate-mapping-2.2′ has invalid child element ‘property’ in namespace ‘urn:nhibernate-mapping-2.2′. List of possible elements expected: ‘meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id’

I tried to step through the source code, and noticed that the downloaded source code version did not match the installed 1.0 RTM of Fluent. So I updated to build 636, which solved this problem, but raised a new one:

The IDbCommand and IDbConnection implementation in the assembly Oracle.DataAccess could not be found.
Ensure that the assembly Oracle.DataAccess is located in the application directory or in the Global Assembly Cache.
If the assembly is in the GAC, use <qualifyAssembly/> element in the application configuration file to specify the full name of the assembly.

Thanks to the interweb I found this blog which provided the solution: Force .Net to load a specific version of ODP (2.111.7.20 in my case), instead of looking for any version:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <qualifyAssembly partialName="Oracle.DataAccess"
      fullName="Oracle.DataAccess, Version=2.111.7.20, Culture=neutral,
                PublicKeyToken=89b483f429c47342"/>
  </assemblyBinding>
</runtime>

Start the Visual Studio prompt and type

gacutil /l | find "Oracle"

to find out which version of ODP you have installed. The assemblies come in a version for .Net 1 and for .Net 2.

This finally caused the BuildSessionFactory to execute successfully, let me create a session and query a table in the database.

I had feared that ODP would not work using the .Net 4 profile, and had thus switched to .Net 2.0 and .Net 3.5 to debug. However, you don’t have Linq in .Net 2, and the Oracle assembly cannot be referenced by projects targeting .Net 3.5 Client Profile and .Net 4.0 Client Profile. Adding the qualified assembly worked in .Net 4, after all.

About these ads

4 Responses to First Steps with NHibernate, Fluent, Oracle, C#, and Visual Studio 2010 RC

  1. […] from: First Steps with NHibernate, Fluent, Oracle, C#, and Visual Studio … If you enjoyed this article please consider sharing […]

  2. […] issues in NHibernate After managing to get NHibernate connect to an Oracle database, the brave developer needs to cope with two specialties of Oracle databases, namely handling […]

  3. […] NHibernate) was pretty straight-forward (with the exception of some nasty error messages). (See part 1, part […]

  4. Eti says:

    Thank you for the help. I have been trying to take this one step further. i.e. to have the application install independent of any Oracle client being installed on the target machine. So far I have collected the 5? dependent dlls and placed them in the bin (1st stop for probe) of the test project – and it works. It does not work with all projects though. Nhibernate type initializer however appears to be looking elsewhere for the Oracle.DataAccess.dll – but am unsure how to confirm this. The result is: ‘The provider is not compatible with the version of Oracle client’ Any thoughts / suggestions would be greatly appreciated. Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 73 other followers

%d bloggers like this: