1. 程式人生 > >sqlite ef6

sqlite ef6

sqli .cn user arch ria des sta 設置 ice

參考文章:

http://www.cnblogs.com/adswads/p/3808549.html

用NUGET 引用 System.Data.SQLite.EF6 app.config 會有默認配置 其中有部分不對.不知道為什麽

錯誤的Config

綠色不需要的雖然本身就是錯的.

invariantName是SQLite type後面用了SqlProviderServices 正確的應該是SQLiteProviderServices

<providers>
<!--<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SQLiteProviderServices, System.Data.SQLite.EF6" />
--> <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SqlProviderServices, System.Data.SQLite.EF6" /> <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> </providers>

正確的providers

    <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"
/> <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> </providers>

DbProviderFactories節點添加

<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" 
invariant="System.Data.SQLite" 
description="Data Provider for SQLite" 
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>

總之有點淩亂.

備忘一個代碼.

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            System.Data.SQLite.SQLiteConnectionStringBuilder connstr = new System.Data.SQLite.SQLiteConnectionStringBuilder(this.Database.Connection.ConnectionString);
            string path = AppDomain.CurrentDomain.BaseDirectory + connstr.DataSource;
            System.IO.FileInfo fi = new System.IO.FileInfo(path);
            if (System.IO.File.Exists(fi.FullName) == false)
            {
                if (System.IO.Directory.Exists(fi.DirectoryName) == false)
                {
                    System.IO.Directory.CreateDirectory(fi.DirectoryName);
                }
                SQLiteConnection.CreateFile(fi.FullName);

                connstr.DataSource = path;
                //connstr.Password = "admin";//設置密碼,SQLite ADO.NET實現了數據庫密碼保護
                using (SQLiteConnection conn = new SQLiteConnection(connstr.ConnectionString))
                {
                    string sql = @" CREATE TABLE User (
   Id INTEGER PRIMARY KEY AUTOINCREMENT,
   Name varchar (20),
   Time timestamp,
   Data blob,
   Val real,
   TestE int);";
                    conn.Open();
                    SQLiteCommand cmd = new SQLiteCommand(sql, conn);
                    cmd.ExecuteNonQuery();
                }
            }
            //添加創建代碼
            //modelBuilder.Configurations.Add(new Blog());
        }

sqlite ef6