EF很强大,可惜对于SQLite不支持CodeFirst模式(需要提前先设计好数据库表结构),不过对SQLite的数据操作还是很好用的。
先用SQLiteManager随便创建一个数据库和一张表:
通过NuGet安装 EF6 和System.Data.SQLite
(会自动把其他三个也装上)
添加配置内容:
数据库连接
1 <connectionStrings> 2 <add name="StudentContext" connectionString="Data Source=StudentDb.sqlite" providerName="System.Data.SQLite.EF6"/> 3 </connectionStrings>
配置文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <configSections> 4 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 5 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 6 </configSections> 7 <connectionStrings> 8 <add name="StudentContext" connectionString="Data Source=StudentDb.sqlite" providerName="System.Data.SQLite.EF6"/> 9 </connectionStrings> 10 <startup> 11 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 12 </startup> 13 <entityFramework> 14 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 15 <providers> 16 <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 17 <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> 18 <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> 19 </providers> 20 </entityFramework> 21 <system.data> 22 <DbProviderFactories> 23 <remove invariant="System.Data.SQLite.EF6" /> 24 <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" /> 25 <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/> 26 </DbProviderFactories> 27 </system.data> 28 </configuration>
测试一下:
时间: 2024-10-03 19:42:52