SQLite + EntityFramework 6 setup
本文关键字:setup EntityFramework SQLite | 更新日期: 2023-09-27 18:10:22
我一直在尝试让EF 6和SQLite在WinForms应用程序中很好地发挥了一个多星期。我似乎遇到了多个随机异常-这让我相信我没有做正确的事情。我似乎找不到合适的安装指南或教程。
我正在使用SQLite 1.0.93和EF 6.1.0直接从Nuget。
这是我的Context类。我不相信DbSet类有问题。
public class MyDbContext: DbContext
{
public MyDbContext() : base("name=dbConnection")
{
System.Data.Entity.Database.SetInitializer<MyDbContext>(null);
}
public DbSet<Object1> Objects1 { get; set; }
public DbSet<Object2> Objects2 { get; set; }
public DbSet<Object3> Objects3 { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions
.Remove<PluralizingTableNameConvention>();
}
}
我也试图用Db数据填充两个单独的DataGridViews。
public static BindingList<Objects1> Objects1BindingList
{
get
{
return MyDbContext.Objects1.Local.ToBindingList();
}
}
public static BindingList<Objects3> Objects3BindingList
{
get { return MyDbContext.Objects3.Local.ToBindingList(); }
}
private static MyDbContext _data;
public static MyDbContext Data
{
get
{
return _data ?? (_data = new MyDbContext());
}
}
我App.config <connectionStrings>
<add name="dbConnection"
connectionString="Data Source=.'data.sqlite" providerName="System.Data.SQLite" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description="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.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.SQLiteProviderServices, System.Data.SQLite.Linq, Version=2.0.88.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</providers>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
谁能给我指一个最近的和经过验证的教程,或者给我指一个更好的方向?
谢谢。
我一直在尝试使用sqlite与asp.net mvc5和EF 6.1,到目前为止没有,但我学到了一些东西,我可以建议你做看看你的app.config文件:
<connectionStrings>
<add name="dbConnection"
connectionString="Data Source=.'data.sqlite" providerName="System.Data.SQLite" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description="Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
<!-- Leave this in there if you want to have EF working with your project -->
<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>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.93.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
<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>
<!-- This only works for sql server localdb edition
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory> -->
</entityFramework>
以上是我在我的网页。配置,让我与我的项目(自web。唯一的问题是,根据http://hintdesk.com/sqlite-with-entity-framework-code-first-and-migration/,你必须在Mozilla中使用SQLite管理器插件创建数据库。
即使当我这样做的时候,我仍然从这个"一个异常类型"系统中得到一些异常。InvalidOperationException"在EntityFramework.dll中发生,但未在用户代码中处理"到其他类型的异常。这真的很糟糕,但也许这能让你振作起来。