Search code examples
wpfsql-server-cemefcaliburn.microlinq-to-sql

Caliburn.Micro + MEF + SQL CE together produce exception Cannot open 'Db\Some.sdf'. Provider 'System.Data.SqlServerCe.3.5' not installed


Hi I am really confuse with this behavior. I use Caliburn.Micro as MVVM framework in my WPF app, MEF is used on export view models classes.

Now I need use SQL compact DB in my app. So I create *dbml file with sqlmetal, because on DB access I would like to use LINQ TO SQL.

First I create simple class which do CRUD operations DB.

Here is it:

public interface IDbManager{}

   [Export(typeof(IDbManager))]
    public class DbManager : IDbManager
    {
        //_dc is DataContext class
        private Spiri_SQL_CE_DB _dc;

        public DbManager()
        {
            //string connStr = System.Configuration.ConfigurationManager.AppSettings["connstr"];
            //_dc = new Spiri_SQL_CE_DB(connStr);

            _dc = new Spiri_SQL_CE_DB(@"Db\Spiri_SQL_CE_DB.sdf");
        }
    }

This class is used in view model class which is WPF window.

 [Export(typeof(IArchiveViewModel))]
    public class ArchiveViewModel :Screen,IArchiveViewModel
    {

        private IDbManager _dbManager;

        [ImportingConstructor]
        public ArchiveViewModel(IDbManager dbManager)
        {
            _dbManager = dbManager;
        }

    }

And this window I open from screen with WindowManager class.

  [Export(typeof(IMessengerViewModel))]
    public class MessengerViewModel : Screen, IMessengerViewModel
    {

        private IWindowManager _windowManager;

        [ImportingConstructor]
        public MessengerViewModel(IWindowManager windowManager)
        {
            _windowManager = windowManager;
            OpenArchive();
        }

        public void OpenArchive()
        {
            var w =  IoC.Get<IArchiveViewModel>();
            _windowManager.ShowWindow(w);
        }

If I run app I get this error:

The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) Cannot open 'Db\Spiri_SQL_CE_DB.sdf'. Provider 'System.Data.SqlServerCe.3.5' not installed.

Resulting in: An exception occurred while trying to create an instance of type 'Spirit.DbManager.DbManager'.

Resulting in: Cannot activate part 'Spirit.DbManager.DbManager'.
Element: Spirit.DbManager.DbManager -->  Spirit.DbManager.DbManager -->  AssemblyCatalog (Assembly="Spirit, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")

Resulting in: Cannot get export 'Spirit.DbManager.DbManager (ContractName="Spirit.DbManager.IDbManager")' from part 'Spirit.DbManager.DbManager'.
Element: Spirit.DbManager.DbManager (ContractName="Spirit.DbManager.IDbManager") -->  Spirit.DbManager.DbManager -->  AssemblyCatalog (Assembly="Spirit, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")


    }

I am really confuse with this error:

1) Cannot open 'Db\Spiri_SQL_CE_DB.sdf'. Provider 'System.Data.SqlServerCe.3.5' not installed.

Beacause:

  • I use Window 7 x64, I have installed SQL Server CE for .NET4.0, .NET3.5
  • WPF use .NET4.0 and is x86
  • In WPF I have reference on assembly system.data.sqlserverce.dll

I create small repo project but I get same error:

Repo project you can find here: http://netload.in/dateiy4s4jdPyCj/DbTest.7z.htm

I think problem code is here:

_dc = new Spiri_SQL_CE_DB(@"Db\Spiri_SQL_CE_DB.sdf");

Spiri_SQL_CE_DB class is from *.dbml file.

I create view model class without MEF I get the same error!

So I try use DbManager class in WPF app without Calibur.Micro and MEF and it works good.

I really don’t know what is bad, I search with google 2 hours but any advice don’t solve this error.

If anybody can help I would be very glad.


Solution

  • Reinstall the 3.5 SP2 MSI, and if you have a x64 system, install both the x86 and x64 MSIs: http://www.microsoft.com/downloads/en/details.aspx?familyid=E497988A-C93A-404C-B161-3A0B323DCE24&displaylang=en