没有App.config的SQLiteentity framework

对于3d-party应用程序插件,必须使用SQLiteentity framework数据库优先方法。 我搜索了所有的互联网,包括添加一个没有App.Config的DbProviderFactory , 使用Entity Framework 6和SQLite的问题等等。 我试图以不同的方式和组合使用它们,但没有任何帮助:

“mscorlib.dll中发生了’System.Data.Entity.Core.MetadataException’类型的未处理exception。附加信息:指定的模式无效。错误:AutosuggestModel.ssdl(2,2):错误0152:没有entity framework提供程序为ADO.NET提供程序找到了名为’System.Data.SQLite.EF6’的不变名。确保提供程序已在应用程序配置文件的’entityFramework’部分注册。“

解决方案中有一个测试控制台应用程序。 使用这个最小的App.config它可以工作:

    

连接字符串已在代码中实现。 二手包是:

         

请提供所有必需的代码并指定插入位置。 提前致谢。

这是一个示例代码,说明了如何实现目标。

 namespace SqliteEFNoConfig { using System.Configuration; using System.Data; using System.Data.Common; using System.Data.Entity; using System.Data.Entity.Core.Common; using System.Data.SQLite; using System.Data.SQLite.EF6; using System.Linq; internal class Program { private static void Main() { // EF manages the connection via the DbContext instantiation // connection string is set in config // Use this code if you want to use a config file // with only the connection string //using (var model = new Model1()) //{ // var dbSetProperty = model.dbSetProperty.ToList(); //} // Alternative method: // Use this code if you don't want to use a config file // You will also need to use the override constructor shown below, // in your EF Model class var connectionString = @"data source = {PathToSqliteDB}"; using (var connection = new SQLiteConnection(connectionString)) { using (var model = new Model1(connection)) { var dbSetProperty = model.dbSetProperty.ToList(); } } } } class SqliteDbConfiguration : DbConfiguration { public SqliteDbConfiguration() { string assemblyName = typeof (SQLiteProviderFactory).Assembly.GetName().Name; RegisterDbProviderFactories(assemblyName ); SetProviderFactory(assemblyName, SQLiteFactory.Instance); SetProviderFactory(assemblyName, SQLiteProviderFactory.Instance); SetProviderServices(assemblyName, (DbProviderServices) SQLiteProviderFactory.Instance.GetService( typeof (DbProviderServices))); } static void RegisterDbProviderFactories(string assemblyName) { var dataSet = ConfigurationManager.GetSection("system.data") as DataSet; if (dataSet != null) { var dbProviderFactoriesDataTable = dataSet.Tables.OfType() .First(x => x.TableName == typeof (DbProviderFactories).Name); var dataRow = dbProviderFactoriesDataTable.Rows.OfType() .FirstOrDefault(x => x.ItemArray[2].ToString() == assemblyName); if (dataRow != null) dbProviderFactoriesDataTable.Rows.Remove(dataRow); dbProviderFactoriesDataTable.Rows.Add( "SQLite Data Provider (Entity Framework 6)", ".NET Framework Data Provider for SQLite (Entity Framework 6)", assemblyName, typeof (SQLiteProviderFactory).AssemblyQualifiedName ); } } } } 

如果您决定不在配置文件中添加连接字符串,则需要在EF模型中添加以下构造函数。

 public Model1(DbConnection connection) : base(connection, true) { } 

注意:以上代码只是如何实现目标的示例,您必须根据需要进行调整。 假设您使用的是EF Code First方法,则提供上述代码。