相關(guān)文章回顧
.net framework 命令行項目使用 sqlite,DbContext
C# .NET EF框架 webapi 安裝使用sqlite
visual studio 2022,ADO.NET 實體數(shù)據(jù)模型添加 sqlite數(shù)據(jù)庫對象
Sqlite安裝
環(huán)境說明
- Visual Studio 2022
- .NET Core 6.0
Nuget安裝
- Microsoft.EntityFrameworkCore.Sqlite
- Microsoft.EntityFrameworkCore.Sqlite.Core
- Newtonsoft.Json
測試程序
ORMContext
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqliteTest2.DB
{
//繼承DbContext,讓EF接管
public class ORMContext : DbContext
{
public DbSet<Student> Students { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//數(shù)據(jù)庫連接字符串
optionsBuilder.UseSqlite("Data Source=blogging.db");
}
}
//測試類
public class Student
{
[Key]//主鍵
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]//自動遞增
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
}
Program.cs
// See https://aka.ms/new-console-template for more information
using Newtonsoft.Json;
using SqliteTest2.DB;
ORMContext _ORMContext = new ORMContext();
//如果沒有數(shù)據(jù)庫,則自動創(chuàng)建
_ORMContext.Database.EnsureCreated();
for(var i = 0;i < 10; i++)
{
_ORMContext.Students.Add(new Student()
{
Name = "Laly" + i,
Age = i,
Sex = "女"
});
}
//保存數(shù)據(jù)庫更新
_ORMContext.SaveChanges();
//打印數(shù)據(jù)
var res = _ORMContext.Students.Where(t => t.Sex == "女").ToList();
Console.WriteLine(JsonConvert.SerializeObject(res));
Console.WriteLine("Hello, World!");
測試結(jié)果
文章來源:http://www.zghlxwxcb.cn/news/detail-502292.html
結(jié)尾
Sqlite3是個特別好的本地數(shù)據(jù)庫,體積小,無需安裝,是寫小控制臺程序最佳數(shù)據(jù)庫。NET Core是.NET 未來的方向,我也最近打算把技術(shù)棧慢慢往那邊遷移。文章來源地址http://www.zghlxwxcb.cn/news/detail-502292.html
到了這里,關(guān)于NET Core添加 Sqlite 數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!