麻烦使用SQLite 1.0.92和Entity Framework 6.1

Ray*_*aya 2 .net c# sqlite xaml entity-framework

我正在尝试将Entity Framework与SQLite数据库文件一起使用,但无法使其正常工作.

我正在使用Visual Studio 2012,SQLite 1.0.92和Entity Framework 6.1.

edmx图正确显示我的表,所以我猜映射有效,但是当我尝试这样做时:

_Context = new DataContext();
var test = _Context.Set<T>().ToList();
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

没有为ADO.NET提供程序找到具有不变名称"System.Data.SQLite"的实体框架提供程序.确保提供程序已在应用程序配置文件的"entityFramework"部分中注册.有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260882.

正如其他SO帖子中所建议的,我尝试在我的DbContext构造函数中添加它以确保复制dll:

var ensureDLLIsCopied = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
Run Code Online (Sandbox Code Playgroud)

结果是,我的程序永远挂起而不是抛出和异常.

我也尝试按照此处的建议更改app.config文件,并在此帖子的接受答案中,但没有更改.

有任何想法吗?

谢谢


编辑:

我改变了我的DbContext:

public class DataContext : DbContext { }
Run Code Online (Sandbox Code Playgroud)

对此:

public class DataContext : DbContext 
{ 
    public DataContext() : base("name=Entities") { }
}
Run Code Online (Sandbox Code Playgroud)

现在,我得到的例外是:

无法在应用程序配置文件中找到"实体"连接字符串

但"实体"的连接字符串在App.config中:

<add name="Entities" 
 connectionString="metadata=res://*/Entities.Model.csdl|res://*/Entities.Model.ssdl|res://*/Entities.Model.msl;provider=System.Data.SQLite;provider connection string=&quot;data source=mypath\dbname.db&quot;"
 providerName="System.Data.EntityClient" />
Run Code Online (Sandbox Code Playgroud)

kjb*_*tel 7

你确定你的app.config设置正确吗?我正在使用EF6.1与System.Data.SQLite v1.0.92.0使用以下app.config:

<?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>
  <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>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>
</configuration>
Run Code Online (Sandbox Code Playgroud)

这将需要在执行程序的app.config中,而不是包含DbContext的程序集的app.config.或者你可以使用我在这里回答的基于代码的配置,你可以将它放在与DbContext相同的程序集中.

如果您的DbContext位于不同的程序集中,那么也是如此,那么该程序集将需要一些引用SQLite的代码,否则编译器将不包含对所需SQLite程序集的引用.您已经在SqlServer上提供了错误的引用,而不是SQLite,这将无法正常工作.