Try   HackMD

dotnet mvc 安裝 sqlite 連動 Entityframework,使用db first建立model

tags: 程式設計 C# MVC 資料庫

免安裝 DB Browser for SQLite

特色:可以直接讀取 SQLite 資料庫 有中文介面
https://sqlitebrowser.org/

主畫面

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

新增、編輯資料表定義schema
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

新增、編輯資料列
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

安裝 SqlCeToolbox 和 SQLite in GAC (一台電腦安裝一次就好)

Install latest Toolbox
Once per Visual Studio edition (daily build at https://github.com/ErikEJ/SqlCeToolbox/wiki/Release-notes )

Install SQLite in GAC

下載
sqlite-netFx46-setup-bundle-x64-2015-1.0.115.5.exe 並安裝,該台電腦安裝一次即可,其他程式可共用

參考來源
Once per machine. Download the latest sqlite-netFx46-setup-bundle-x86-2015-1.0.xxx.0.exe (from https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki)

Select "Full Installation"

Select: Install the assemblies into the global assembly cache - Install VS designer components

nuget 安裝以下套件 (每個專案要各自安裝)

* sqlite (這邊是3.13版)

  • System.Data.SQLite.SQLite
  • System.Data.SQLite.EF6

### sqlite (這邊是3.13版)
~~ https://www.nuget.org/packages/SQLite/3.13.0?_src=template

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

System.Data.SQLite 和 System.Data.SQLite.EF6

System.Data.SQLite 相依於 enfityframework 6.3以上,寫這篇的時候,enfityframework繁體中文套件只有6.2,直接升級ef有相容性問題升不上去,因此要 手動刪掉EntityFramework.zh-Hant 中文套件,才能把ef核心升級上去(升級之後可以再回頭安裝中文套件不用擔心),升到 ef 6.3 才能安裝這兩個關鍵的元件
System.Data.SQLite.SQLite
System.Data.SQLite.EF6

裝 System.Data.SQLite.SQLite 會順便裝這一堆

正在安裝:

SQLitePCLRaw.lib.e_sqlite3.2.0.2
System.Buffers.4.4.0
System.Numerics.Vectors.4.4.0
System.Runtime.CompilerServices.Unsafe.4.5.2
System.Memory.4.5.3
SQLitePCLRaw.core.2.0.2
Microsoft.Data.Sqlite.Core.3.1.4
SQLitePCLRaw.provider.dynamic_cdecl.2.0.2
SQLitePCLRaw.bundle_e_sqlite3.2.0.2
Microsoft.Data.Sqlite.3.1.4

不用安裝 Microsoft.Data.Sqlite

要安裝的是 System.Data.SQLite ( https://www.nuget.org/packages/System.Data.SQLite/1.0.113.1?_src=template )
,要注意不要安裝到 Microsoft.Data.Sqlite,會有相衝問題,不小心裝到可以從nuget直接移除掉

安裝成功之後

在要建立 Model 的資料夾點選右鍵 > 新增項目 > 在新開的視窗選 [新增 ADO.NET 實體資料模型] (注意下方的名稱不要衝突囉)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


這次的目標是從資料庫生成model,,所以在這個視窗選 [來自資料庫的 EF Designer] ,如果想使用code First的可以用 code first的方式,但在這篇不討論作法

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


[新增連接] > 資料來源選 [變更]

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

變更資料來源選擇 [SQLite Provider],如果沒有這個選項,代表前面安裝元件有問題,請確認是否都正常安裝了

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

更換web.config的連線字串,使用相對路徑

範例1,在C#程式裡控制
String currentPath = System.IO.Path.GetDirectoryName( Application.ExecutablePath );

  string relativePath = @"database\myfile.s3db";
    string currentPath;
    string absolutePath;
    string connectionString;

    currentPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
    absolutePath = System.IO.Path.Combine(currentPath,relativePath);

    connectionString = string.Format("DataSource={0}", absolutePath);

    SQLiteConnection cnn = new SQLiteConnection(connectionString);

範例2 在web.config
使用 Data Source=|DataDirectory|mydb.db
這樣就代表資料庫在專案資料夾的 /App_Data/mydb.db

<add name="mainEntities1" connectionString="metadata=res://*/Models.Model1_sqlite.csdl|res://*/Models.Model1_sqlite.ssdl|res://*/Models.Model1_sqlite.msl;provider=System.Data.SQLite.EF6;provider connection string=&quot;data source=|DataDirectory|sqlite.db&quot;" providerName="System.Data.EntityClient" />

https://www.itdaan.com/tw/c038ccba03ca68debe5bdd0d97784bf1

參考來源

https://docs.microsoft.com/zh-tw/ef/core/get-started/?tabs=visual-studio

https://blog.csdn.net/catshitone/article/details/80320493

https://ronsun.github.io/content/20190907-use-sqlite-with-ef-and-vs2017.html

https://github.com/ErikEJ/SqlCeToolbox/wiki/EF6-workflow-with-SQLite-DDEX-provider

[研究] NuGet 無法解析相依性。'EntityFramework 6.4.0' 與 'EntityFramework.zh-Hant 6.2.0 條件約束: EntityFramework (= 6.2.0)' 不相容。
http://shaurong.blogspot.com/2019/12/nuget-entityframework-640.html