SqlServeCe and NHibernate

633 views
Skip to first unread message

Dathan

unread,
May 31, 2009, 10:14:32 PM5/31/09
to nhusers
I'm trying to port a project I've previously had running against a SQL
Server 2005 database to use SQL Server CE instead. I'm trying to use
the following code:

configuration = new Configuration();

configuration.SetProperty
(NHibernate.Cfg.Environment.ConnectionDriver,
"NHibernate.Driver.SqlServerCeDriver");
configuration.SetProperty
(NHibernate.Cfg.Environment.Dialect,
"NHibernate.Dialect.MsSqlCeDialect");
configuration.SetProperty
(NHibernate.Cfg.Environment.ConnectionProvider,
"NHibernate.Connection.DriverConnectionProvider");
configuration.SetProperty
(NHibernate.Cfg.Environment.ConnectionString, String.Format
("DataSource={0}", filename));
configuration.SetProperty
(NHibernate.Cfg.Environment.ShowSql, "true");
configuration.SetProperty
(NHibernate.Cfg.Environment.ReleaseConnections, "on_close");

// now the mappings
configuration.AddXmlFile("Mappings\\Player.hbm.xml");

factory = configuration.BuildSessionFactory();

I'm receiving the following error at the final line:
An unhandled exception of type 'NHibernate.HibernateException'
occurred in NHibernate.dll

Additional information: Could not create the driver from
NHibernate.Driver.SqlServerCeDriver.

Can anyone point me out where I'm going wrong?

Thanks!

~Dathan

Dathan Bennett

unread,
May 31, 2009, 11:26:28 PM5/31/09
to nhusers
I've narrowed it down to the ConnectionDriver.ConfigureDriver method.  The call to ReflectorHelper.ClassForName returns what looks like the right class, but then Activator.CreateInstance throws a TargetInvocationException with the InnerException being one of NHibernate.HibernateException with message "The IDbCommand and IDbConnection implementation in the assembly System.Data.SqlServerCe could not be found. Ensure that the assembly System.Data.SqlServerCe 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."

The innermost exception was thrown by ReflectHelper.TypeFromAssembly at a call to Assembly.Load("System.Data.SqlServerCe"): FileNotFoundException...  I dunno why it would fail to find the assembly, though.  It's not a framework version mismatch, and I'm pretty sure the SqlServerCe assembly is in the GAC...  I stuck an identical line (Assembly assy = Assembly.Load("System.Data.SqlServerCe");) into the test program, and it failed there, too.  This is clearly not an NHibernate problem -- is this a system configuration issue?  Has anyone encountered it before?

~Dathan

Artur

unread,
Jun 1, 2009, 4:25:52 AM6/1/09
to nhusers
Hi

Are you sure you want to rely on the necessary assemblies being in the
GAC?
I think, this way you lose some control over your application and
expose yourself to various problems, e.g. versioning.

Sql Server Ce can be privately deployed with the application. You can
find many materials on this on the web.
A good source of information on SqlServerCe is Steve Lasker's blog,
for example you can find info on private deployment in this blog post:
http://blogs.msdn.com/stevelasker/archive/2008/10/22/privately-deploying-sql-server-compact-with-the-ado-net-entity-provider.aspx

Artur Dorochowicz

Dathan Bennett

unread,
Jun 1, 2009, 3:49:47 PM6/1/09
to nhu...@googlegroups.com
Actually, this might be an NH issue.  Looking up the documentation for Assembly.Load(string), I see "Loads an assembly given the long form of its name. "  Sure enough, when I replace my test case (Assembly.Load("System.Data.SqlServerCe");) with the long version of the assembly name (Assembly.Load("System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");) it works fine.  But the NH call that throws the error uses the first call.  Is this a bug in NHibernate, or is my system just missing a config step that allows it to resolve "System.Data.SqlServerCe" to the appropriate assembly?

~Dathan

John Rayner

unread,
Jun 1, 2009, 5:56:44 PM6/1/09
to nhusers
You spotted the inner exception that said you need to use
<qualifyAssembly> if System.Data.SqlServerCe is deployed in the GAC.
Have you tried that?

FYI docs for <qualifyAssembly> are at http://msdn.microsoft.com/en-us/library/cd71chf0.aspx.
There's an example of someone using it for Firebird at
http://stackoverflow.com/questions/379748/how-do-i-setup-nhibernate-with-visual-studio-and-firebird.

Cheers,
John

On Jun 1, 8:49 pm, Dathan Bennett <dat...@gmail.com> wrote:
> Actually, this might be an NH issue.  Looking up the documentation for
> Assembly.Load(string), I see "Loads an assembly given the long form of its
> name. "  Sure enough, when I replace my test case
> (Assembly.Load("System.Data.SqlServerCe");) with the long version of the
> assembly name (Assembly.Load("System.Data.SqlServerCe, Version=3.5.1.0,
> Culture=neutral, PublicKeyToken=89845dcd8080cc91");) it works fine.  But the
> NH call that throws the error uses the first call.  Is this a bug in
> NHibernate, or is my system just missing a config step that allows it to
> resolve "System.Data.SqlServerCe" to the appropriate assembly?
>
> ~Dathan
>
>
>
> On Mon, Jun 1, 2009 at 3:25 AM, Artur <ArturDorochow...@gmail.com> wrote:
>
> > Hi
>
> > Are you sure you want to rely on the necessary assemblies being in the
> > GAC?
> > I think, this way you lose some control over your application and
> > expose yourself to various problems, e.g. versioning.
>
> > Sql Server Ce can be privately deployed with the application. You can
> > find many materials on this on the web.
> > A good source of information on SqlServerCe is Steve Lasker's blog,
> > for example you can find info on private deployment in this blog post:
>
> >http://blogs.msdn.com/stevelasker/archive/2008/10/22/privately-deploy...
> > > > ~Dathan- Hide quoted text -
>
> - Show quoted text -

Dathan Bennett

unread,
Jun 1, 2009, 6:46:45 PM6/1/09
to nhu...@googlegroups.com
Doh!  I completely missed that text.  Sure enough, adding
<runtime><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><qualifyAssembly partialName="System.Data.SqlServerCe" fullName="System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/></assemblyBinding></runtime>
to app.config has it working like a charm.

Thanks!

~Dathan
Reply all
Reply to author
Forward
0 new messages