加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何用Entity Framework 6 连接Sqlite数据库

发布时间:2020-12-12 19:55:15 所属栏目:百科 来源:网络整理
导读:获取Sqlite 1.可以用NuGet程序包来获取,它也会自动下载EF6 2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 注意这里面每个.net framework都有两个版本,一个带有bundle字眼,一个没有。一个安装

获取Sqlite

1.可以用NuGet程序包来获取,它也会自动下载EF6

2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

注意这里面每个.net framework都有两个版本,一个带有bundle字眼,一个没有。一个安装的DLL里面包含SQLite.Interop.dll,而另一个没有。如果你运行代码的时候报

“无法加载SQLite.Interop.dll”的错误,则将安装文件中的SQLite.Interop.dll拷贝到Bin文件中即可。或是在NuGet下载的packagesSystem.Data.SQLite.Core.1.0.94.0build中也有对应的程序。

示例代码

Model.cs

   public class Person
    {
        public Int64 Id { get; set; } //注意要用Int64
        public string FirstName { set; }
        string LastName { set; }
    }

    class MyContext : DbContext
    {
        public DbSet<Person> Persons { set; }

        public MyContext()
            : base("SqliteTest")
        {

        }
    }

Program.cs

 static void Main(string[] args)
        {
            MyContext context = new MyContext();
            var empList = context.Persons.OrderBy(c => c.FirstName).ToList();
            Console.WriteLine(empList.Count);

            Person people = new Person()
            {
                FirstName = Hello",LastName = World"
            };
            context.Persons.Add(people);
            context.SaveChanges();
            Console.ReadLine();
        }

示例代码很简单,就是用EF对Person表进行新增与查看。

配置config文件

如果你是用NuGet获取Sqlite,会自动在config中配置一些相关的信息。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  configSections>
    <!-- For more information on Entity Framework configuration,visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version=6.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </>
   connectionStringsadd ="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" startupsupportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" system.dataDbProviderFactories>
      ="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite"="System.Data.SQLite.SQLiteFactory,System.Data.SQLite" />
      remove invariant="System.Data.SQLite" ="SQLite Data Provider (Entity Framework 6)"="System.Data.SQLite.EF6"=".NET Framework Data Provider for SQLite (Entity Framework 6)"="System.Data.SQLite.EF6.SQLiteProviderFactory,System.Data.SQLite.EF6" />
    entityFrameworkdefaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory,EntityFramework"parameters>
        parameter value="v11.0" defaultConnectionFactoryprovidersprovider invariantName="System.Data.SqlClient"="System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer" ="System.Data.SQLite.EF6.SQLiteProviderServices,255); line-height:1.5!important">>
>

其中数据连接串是:

   />

注意提供程序集是System.Data.SQLite.EF6。

但是这个配仍然是错误的。

如果此时运行程序,会报错:

Unable to determine the provider name for provider factory of type 'System.Data.SQLite.SQLiteFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.

或中文错误信息:

未找到具有固定名称“System.Data.SQLite”的 ADO.NET 提供程序的实体框架提供程序。请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序。

意思是EF没有找到提供System.Data.SQLite.SQLiteFactory的dll,我们看看现在config中的entityFramework节点:

  >

有System.Data.SQLite.EF6与System.Data.SqlClient,确实没有名称为System.Data.SQLite的提供程序。这里我一直不明白为什么sqlite会去找名称为System.Data.SQLite的提供程序,因为我们在连接串中配置的provider也是System.Data.SQLite.EF6。

那我们就在EF的配置节点中增加一个名为System.Data.SQLite的provider,但type仍然是System.Data.SQLite.EF6。最终的配置如图:

红色部分是配置有变化的地方。

这里再运行程序就可以了。

注意:

1.连接串的配置。

数据连接串可以指定绝对地址,也可以指定相对地址。像我的data source=SqliteTest.db,则SqliteTest.db要在Bin文件夹中,如果是web程序可以通过Data Source=|DataDirectory|SqliteTest.db来配置在App_Data文件平中。

2.如果没有指定数据库中的表文件名,EF生成的SQL表都是用复数表示。就像我的程序中实体名是Person,但EF去查找的表名会是People。所以在数据库中定义的表名是People。

3.不支持CodeFirst模式,您需要自己先设计好Sqlite的表结构。

示例代码(packages文件太大,所以删除了):SqliteLinqTest.zip

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读