0

嗨,我真的对这种行为感到困惑。我在我的 WPF 应用程序中使用 Caliburn.Micro 作为 MVVM 框架,MEF 用于导出视图模型类。

现在我需要在我的应用程序中使用 SQL compact DB。所以我用 sqlmetal 创建 *dbml 文件,因为在访问数据库时我想使用 LINQ TO SQL。

首先,我创建简单的类来执行 CRUD 操作 DB。

就这个:

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");
        }
    }

此类用于视图模型类,即 WPF 窗口。

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

        private IDbManager _dbManager;

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

    }

我使用 WindowManager 类从屏幕打开这个窗口。

  [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);
        }

如果我运行应用程序,我会收到此错误:

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


    }

我真的对这个错误感到困惑:

1) 无法打开“Db\Spiri_SQL_CE_DB.sdf”。未安装提供程序“System.Data.SqlServerCe.3.5”。

因为:

  • 我使用 Window 7 x64,我已经为 .NET4.0、.NET3.5 安装了 SQL Server CE
  • WPF 使用 .NET4.0 并且是 x86
  • 在 WPF 中,我参考了程序集system.data.sqlserverce.dll

我创建了小型回购项目,但我得到了同样的错误:

您可以在这里找到回购项目:http: //netload.in/dateiy4s4jdPyCj/DbTest.7z.htm

我认为问题代码在这里:

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

Spiri_SQL_CE_DB 类来自 *.dbml 文件。

我在没有 MEF 的情况下创建视图模型类我得到了同样的错误!

所以我尝试在没有 Calibur.Micro 和 MEF 的 WPF 应用程序中使用 DbManager 类,它运行良好。

我真的不知道什么是坏的,我用谷歌搜索了 2 个小时,但任何建议都不能解决这个错误。

如果有人可以帮助我会很高兴。

4

1 回答 1

2

重新安装 3.5 SP2 MSI,如果您有 x64 系统,请同时安装 x86 和 x64 MSI:http ://www.microsoft.com/downloads/en/details.aspx?familyid=E497988A-C93A-404C-B161- 3A0B323DCE24&displaylang=en

于 2011-01-29T13:08:16.797 回答