Невозможно определить имя поставщика для поставщика factory типа "System.Data.Sqlite.SqliteFactory"
Я хочу использовать инфраструктуру сущностей sqlite в моем веб-проекте api, но он всегда не может работать хорошо,
вот моя среда разработки.
1.Визуальная студия 2013,.net framework 4.5
-
версия пакета sqlite 1.0.97, я установлен ниже пакетов
system.data.sqlite,
system.data.sqlite.ef6,
system.data.sqlite.linq
-
EntityFramework - это 6.1.3
Вот исключение, которое я получил
Невозможно определить имя поставщика для поставщика factory типа "System.Data.SQLite.SQLiteFactory". Убедитесь, что поставщик ADO.NET установлен или зарегистрирован в конфигурации приложения
Вот мой webconfig
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> <!--type="System.Data.SQLite.EF6.SQLiteProviderServices-->
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="Sqlite" connectionString="data source="D:\MyWebAPI\src\MyWeb.Api\App_Data\sqlite_test.db"" providerName="System.Data.SQLite.EF6" />
</connectionStrings>
</configuration>
Ответы
Ответ 1
Я получил ответ сам, но я до сих пор не знаю первопричины, теперь он работает. Я изменил webconfig, вот webconfig, который заставляет мой проект работать.
Я добавил поставщика, который является "System.Data.Sqlite", обратите внимание на его тип, который аналогичен System.Data.Sqlite.EF6
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
<remove invariant="System.Data.SQLite.EF6" />
Здесь все настроено.
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
</DbProviderFactories>
</system.data>
Ответ 2
Это был супер-великий ответ zhnglicho!
Другой вариант, помимо размещения поставщиков и DbProviderFactories в app.config или web.config, заключается в том, чтобы закодировать ваши ProviderFactories и использовать его в вашей реализации DbContext.
Usings:
using System.Data.Entity;
using System.Data.Entity.Core.Common;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.SQLite;
using System.Data.SQLite.EF6;
Класс конфигурации:
public class SQLiteConfiguration : DbConfiguration
{
public SQLiteConfiguration()
{
SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
}
}
Поместите это в свой конструктор:
public PythonContext() : base($"name=pythonSource")
{
DbConfiguration.SetConfiguration(new SQLiteConfiguration());
}
Я запускал модульные тесты для обоих методов (этот и ответ zhnglicho), и я не нашел различий в скорости.
Отладочный вывод (получение количества записей и случайной цитаты из sqlitedb):
Found 18307 Records
Presenter: 'And Miles Yellowbird, up high in banana tree, the golfer and
inventor of Catholicism.'
Debug Trace:
Native library pre-loader is trying to load native SQLite library "C:\Users\***\***\\SQLiteTests\bin\Debug\x64\SQLite.Interop.dll"...
Я проголосовал за предыдущий ответ, поскольку он работал у меня, но это всего лишь предположение, если вы не хотите подключаться к своим конфигам. ~ МИР!
sqlite entityframework