Vikram Bose
Vikram Bose

Reputation: 3365

Can't create schema for SQL Server CE using Nhibernate

When I am creating a schema for SQL Server CE using Nhibernate with this code:

Fluently.Configure()
            .Database(MsSqlCeConfiguration.Standard
            .ConnectionString(c => c.Is("Data Source=" + file))
            .Dialect<NHibernate.Dialect.MsSqlCeDialect>()
            .Driver<NHibernate.Driver.SqlServerCeDriver>()
            .ShowSql())
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateSessionFactory>())
            .ExposeConfiguration(BuildSchema)
            .BuildSessionFactory();  

private static void BuildSchema(Configuration config)
    {
       // new SchemaExport(config).Drop(false, true);
        //new SchemaExport(config).Create(true, true);

         //If DB File does not exists, create it.
        if (!File.Exists(file))
        {
            Directory.CreateDirectory(Path.GetDirectoryName(databaseFileName));
            SqlCeEngine engine = new SqlCeEngine("Data Source="+ file);
            engine.CreateDatabase();
            // this NHibernate tool takes a configuration (with mapping info in)
            // and exports a database schema from it
            new SchemaExport(config).Execute(false, true, false);
            //FormulasDAO.AddDefaultFormulaCollection();
        }
        else
        {
            new SchemaUpdate(config).Execute(false, true);
        }
    }

I got an exception like this

An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

Inner exception is

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 element in the application configuration file to specify the full name of the assembly.

Help for this problem.

Upvotes: 3

Views: 1792

Answers (1)

Vikram Bose
Vikram Bose

Reputation: 3365

Actually the problem was, in GAC there are 2 version of dlls so Nhibernate didnt know which dll need to use because NHibernate taking the dll from GAC using dll name only not using version name.

So need to tell NHibernate in the AppConfig

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="System.Data.SqlServerCe" fullName="System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
</assemblyBinding>

Upvotes: 7

Related Questions