11

I'm working on a system to use a SqlServerCe with NHibernate. From my driver program, if I add the System.Data.SqlServerCe assembly as a reference, I can create and run queries against a database just fine. When trying to use NHibernate, though, I get the following exception:

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll Additional information: Could not load file or assembly 'System.Data.SqlServerCe' or one of its dependencies. The system cannot find the file specified.

I've traced the exception to a call to Assembly.Load("System.Data.SqlServerCe"), which seems like it should work. The System.Data.SqlServerCe assembly is in the GAC (I've also tried to add it as a local reference with CopyLocal=true, to no avail), and I can use its members fine, so why can't I explicitly load it? When I open the assembly in Reflector, it has trouble loading the System.Transactions reference (I've also tried adding it as a local reference, again to no avail), so loading that assembly might be the problem, rather than the System.Data.SqlServerCe assembly.

Is this a common problem? System misconfiguration, maybe?

Noctis
  • 11,507
  • 3
  • 43
  • 82
Dathan
  • 7,266
  • 3
  • 27
  • 46
  • 1
    Looks like you never heard about Fusion: http://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx – Andriy Volkov Jun 01 '09 at 19:41
  • Ooh, neat tool. The feedback it gives me is: LOG: The same bind was seen before, and was failed with hr = 0x80070002. ERR: Unrecoverable error occurred during pre-download check (hr = 0x80070002). Which doesn't seem all that useful -- it's failed before, so let's fail again? I need to read up on the fusion docs to hopefully figure this out a little better. I was pointed to a solution to my problem, however -- details below. – Dathan Jun 01 '09 at 22:39

3 Answers3

8

Apparently this can be solved by adding a <qualifyAssembly> element to the app.config file. Adding the following has my app running smoothly:

<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>

Thanks!

Dathan
  • 7,266
  • 3
  • 27
  • 46
  • yeah the fullName referred to above is indeed the full name for the assembly. It gives the CLR the info it needs to find and load the specific assembly. You can also substitute that fullName in a connection string in a .config file (in place of the partialName) – nocache Jun 03 '09 at 03:00
2

This is most probably connected to some system (mis)configuration.

However, by design SQL Server CE is just a single DLL, which may be shipped together with your product.
This means that you can just set Copy local to True in the reference properties of System.Data.SqlServerCe, and you are done.

rob
  • 36,896
  • 2
  • 55
  • 65
1

I had a similar problem. The mistake I was doing was I was trying to execute the .exe present in the ../obj/x86/Release, whereas I am supposed to execute the .exe present in ../bin/Release. (I am absolute newbie to C#).

(I also noticed that in this ../bin/Release directory , the referenced .dll file is copied locally.)

Bhushan
  • 18,329
  • 31
  • 104
  • 137