无法确定“System.Data.Sqlite.SqliteFactory”类型的提供程序工厂的提供程序名称
我想在我的web api项目中使用sqliteentity framework,但它始终无法正常工作,
这是我的发展环境。
1.Visual studio 2013,.net framework 4.5
-
sqlite包的版本是1.0.97,我在下面安装了包
system.data.sqlite,system.data.sqlite.ef6,system.data.sqlite.linq
-
EntityFramework是6.1.3
这是我得到的例外
无法确定“System.Data.SQLite.SQLiteFactory”类型的提供程序工厂的提供程序名称。 确保在应用程序配置中安装或注册了ADO.NET提供程序
这是我的webconfig
我可以通过visual studio工具中的“连接数据库”连接sqlite数据库文件,我也可以先用代码生成实体。
但我无法获得数据正常
这是我的代码
public partial class Sqlite : DbContext { public Sqlite() : base("name=Sqlite") { } public virtual DbSet AuthorizationLogs { get; set; } public virtual DbSet Checksums { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity() .Property(e => e.ClientKey) .IsUnicode(false); modelBuilder.Entity() .Property(e => e.Login) .IsUnicode(false); modelBuilder.Entity() .Property(e => e.Password) .IsUnicode(false); modelBuilder.Entity() .Property(e => e.ConnectionString) .IsUnicode(false); } }
public class ValuesController : ApiController { // GET api/values Sqlite ctx = new WebApi2Demo.Sqlite(); public IEnumerable Get() { return ctx.Checksums; } }
我自己得到了答案,但我仍然不知道根本原因,现在它有效。 我更改了webconfig,这是使我的项目工作的webconfig。
我添加了一个“System.Data.Sqlite”提供程序,请注意它的类型与System.Data.Sqlite.EF6相同
这是所有配置。
这是一个非常好的答案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()); }
我对这两种方法都进行了unit testing(这个和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