以前一直用sqlite.net 1.0.66.0版本,在.net4下面程序寫好了部署到目的地機器時winform程序總是出現缺少運行時的問題。有時裝了運行時也還是出問題,后來發現是混合模式的問題,當時是增加以下配置解決的:
<startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/> </startup>
事隔一年后,使用sqlite.net 1.0.89.0又遇到這個問題,這次加了這個也沒效果,而且不報錯提示缺少dll等錯誤,單單抓到報“System.Threading.ThreadExceptionEventArgs”尼瑪我人品差啊,這是何錯誤,這個異常的范圍很廣的有木有! 下載運行時安裝了跑demo正常,但是程序還是報錯。
問題鎖定:
肯定還是配置問題。經過一番艱苦排查,終於發現是使用了EF for sqliite,必須在配置文件理加入以下節點:
<system.data> <DbProviderFactories> <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, Version=1.0.89.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" /> </DbProviderFactories> </system.data>
總結:
人品有問題啊,老是解決了問題不求甚解不去找原理,這不科學啊!
原因是EF使用SQLite Data Provider在構造DbProviderFactories時需要從配置里讀取sqlite的版本等信息,然后才能正常實例化ObjectContext對象。
另外,還遇到妖怪的事:
using (ScanLocalDbEntities db = new ScanLocalDbEntities(dbConnStr)) { db.DBExecute("delete from ImportPrdSKU_Info"); db.DBExecBatchSQL(sqls); System.Threading.Thread.Sleep(300); db.Refresh(System.Data.Objects.RefreshMode.StoreWins,db.SKU_Info); db.Connection.Close(); }
using之后用文件流操作那數據庫文件,老說有另一個線程占用。難道是沒Close Connect?關閉了也不行。超出我常識了,暈了。基礎不好啊,到底會寫程序不大哥?!
加了個 GC.Collect();正常了...