基础不存在,我通过 Code First 来完成。数据库和所有表都必须由模型类自动创建。
我通过 NuGet 连接 System.Data.SQLite.EF6,编写连接配置,创建模型、上下文,然后是错误。
配置(自动创建,我只注册了 connectionStrings):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<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" />
</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="DbConnection" connectionString="Data Source=C:\test.db;Version=3" providerName="System.Data.SQLite.EF6" />
</connectionStrings>
</configuration>
语境:
public class ProjectContext : DbContext
{
public ProjectContext() : base("DbConnection")
{
}
public DbSet<Account> Accounts { get; set; }
}
结果,它在 public ProjectContext() : base("DbConnection") 行抛出异常
mscorlib.dll 中类型为“System.InvalidOperationException”的未处理异常
更多信息:实体框架提供程序类型“System.Data.SQLite.EF6.SQLiteProviderServices,System.Data.SQLite.EF6”在 ADO.NET 提供程序的应用程序配置文件中注册,名称为“System.Data.SQLite.EF6” '无法加载。确保使用程序集限定名称并且该程序集可用于正在运行的应用程序。有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260882。
请告诉我,你哪里搞砸了?
UPD:添加 System.Data.SQLite with NuGet,上下文创建正常。但是,它现在在尝试将模型对象添加到上下文的 DbSet 时抛出异常:
找不到具有不变名称“System.Data.SQLite”的 ADO.NET 提供程序的实体框架提供程序。
还需要从 Nuget 中提取库以
EF与 c一起工作SQLite,特别是System.Data.SQLite。就我个人而言,我这样做了,虽然有点不时髦,但它确实有效。
在
App.config(或Web.config万一ASP)