获取Sqlite

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

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

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下载的packages\System.Data.SQLite.Core.1.0.94.0\build中也有对应的程序。

示例代码

Model.cs

如何用Entity Framework 6 连接Sqlite数据库[转]
   public class Person
    {
        public Int64 Id { get; set; } //注意要用Int64
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

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

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

        }
    }
如何用Entity Framework 6 连接Sqlite数据库[转]

Program.cs

如何用Entity Framework 6 连接Sqlite数据库[转]
 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();
        }
如何用Entity Framework 6 连接Sqlite数据库[转]

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

配置config文件

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

如何用Entity Framework 6 连接Sqlite数据库[转]
<?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" />
  </configSections>
   <connectionStrings>
    <add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.data>
    <DbProviderFactories>
      <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" />
      <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.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>
</configuration>
如何用Entity Framework 6 连接Sqlite数据库[转]

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-07-01
  • 2021-05-28
  • 2022-02-21
猜你喜欢
  • 2021-11-01
  • 2022-12-23
  • 2021-07-02
  • 2021-12-05
  • 2021-04-18
相关资源
相似解决方案