Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 901
Vikram Bose Avatar asked Apr 22 '26 10:04

Vikram Bose


1 Answers

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>

like image 152
Vikram Bose Avatar answered Apr 24 '26 22:04

Vikram Bose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!