前段时间试着用Entity Framework for Sqlite环境,发现了一些坑坑洼洼,记录一下。
  同时试了一下配置多种数据库,包括Sqlite、Sql Server、Sql Server LocalDB、Sql Server Compact。
  我建的demo项目结构以及通过NuGet安装的包:

  EFDemo.MultipleDB.UI引用了EFDemo.MutipleDB项目。
  1. 遇到的异常1

  The Entity Framework provider type 'System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SQLite.EF6' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
  我是是引用了Sqlite相关的dll了的(见下图),但是发现生成后再EFDemo.MultipleDB.UI下的生成文件中没有自动将这些dll拷贝过来。

  于是我将这些手动拷贝过来了。
  2. 遇到的异常2
  刚才的异常没了,又出现了一个异常。
  The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
  这次在代码里面加了这么一段,不用调用,只是在代码里让它存在:
  /// <summary>
  /// 解决provider不能自动加载的问题
  /// (不需要调用,放在这里即可)
  /// </summary>
  private static void FixProvidersNotAutoLoadProblem()
  {
  var _ = typeof(System.Data.SQLite.EF6.SQLiteProviderFactory);
  var __ = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
  var ___ = typeof(System.Data.Entity.SqlServerCompact.SqlCeProviderServices);
  }