標籤:
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>
測試一下:
EntityFramework串連SQLite