数据库
首页 > 数据库> > sqlite ef6

sqlite ef6

作者:互联网

原文链接:http://www.cnblogs.com/yueyue184/p/6809494.html

参考文章:

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());
        }

 

转载于:https://www.cnblogs.com/yueyue184/p/6809494.html

标签:sqlite,System,ef6,IO,fi,new,connstr,SQLite
来源: https://blog.csdn.net/weixin_30699955/article/details/99064137