SQLite is needed in recent projects, and other features in the project are data access implemented by Ef+sqlserver. So, want to use EF to visit SQLite, two more troublesome places.
First: EF Connect SQLite profile needs to be changed manually
<entityFramework> <defaultconnectionfactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v13.0"/> </parameters> </defaultConnectionFactory> <providers> <provider invariantname="System.Data.SqlClient"Type="System.Data.Entity.SqlServer.SqlProviderServices, Entityframework.sqlserver"/> <provider invariantname="System.Data.SQLite"Type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>//here, after adding the DLL for SQLite's EF through NuGet, the resulting is: System.Data.SQLite.EF6, change to this now. </providers> </entityFramework> <system.data> <DbProviderFactories> <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"/> <remove invariant="System.Data.SQLite"/> <add name="SQLite Data Provider"Invariant="System.Data.SQLite"description=". NET Framework Data Provider for SQLite"Type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/> </DbProviderFactories> </system.data>
Second: EF6 does not support SQLite Code first build database by default, it needs to expand itself.
Public Partial classSqlitedatamodels:dbcontext {Private string_dbpath; PublicSqlitedatamodels (stringpath):Base(Newsqliteconnection {ConnectionString=NewSqliteconnectionstringbuilder {DataSource=Path, ForeignKeys=false, Binaryguid=false, }. ConnectionString},true) {_dbpath=path; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {modelBuilder.Conventions.Remove<PluralizingTableNameConvention> ();//Database.setinitializer (NewSqlitecontextinitializer<sqlitedatamodels> (_dbpath, ModelBuilder));//This sentence is the key. Custom database context initialization settings Base. Onmodelcreating (ModelBuilder); } Public VirtualDbset<t_analysisbooks> T_analysisbooks {Get;Set; } Public VirtualDbset<t_generaledgerstatistics> T_generaledgerstatistics {Get;Set; } Public VirtualDbset<t_ledgerclassificationchart> T_ledgerclassificationchart {Get;Set; } } classSqlitecontextinitializer<t>: idatabaseinitializer<t>whereT:dbcontext {BOOL_dbexists; Dbmodelbuilder _modelbuilder; PublicSqlitecontextinitializer (stringDbPath, Dbmodelbuilder modelBuilder) {_dbexists=file.exists (DbPath); _modelbuilder=ModelBuilder; } Public voidInitializedatabase (T context) {if(_dbexists)return; varModel =_modelbuilder.build (context. Database.connection); using(varXACT =context. Database.begintransaction ()) {Try{createdatabase (context. Database, model); Xact.commit (); } Catch(Exception ex) {xact. Rollback (); Throw; } } } Private voidCreateDatabase (Database db, Dbmodel model) {Const stringTabletmpl ="CREATE TABLE [{0}] (\n{1}\n);"; Const stringColumntmpl ="[{0}] {1} {2}";//name, type, Decl foreach(varTypeinchmodel. Storemodel.entitytypes) {varDefs =Newlist<string>(); //PRIMARY Key varKeys = type. Keyproperties.select (x =x.name). ToList (); foreach(varPinchtype. Properties) {varDecls =Newhashset<string>(); //Create Column AutoIncrement if(keys. Contains (P.name.tostring ())) {decls. ADD ("INTEGER PRIMARY KEY autoincrement"); Defs. ADD (string. Format (Columntmpl, P.name,"",string. Join (" ", DECLS))); } Else { if(!p.nullable) decls. ADD ("Not NULL"); Defs. ADD (string. Format (Columntmpl, P.name, P.typename,string. Join (" ", DECLS))); } } //Create a table varsql =string. Format (Tabletmpl, type. Name,string. Join (", \ n", defs)); Db. Executesqlcommand (SQL); } } }
EF6 does not support SQLite Code first Solutions