.Net Core控制台程序使用EF Core读写SQLite
开发工具:vs2019;操作系统:win10
代码下载地址:
链接:https://pan.baidu.com/s/1dv37YxXCqwnTpRfeputc8A
提取码:6a2l
1、Nuget包的引用
新建一个名为ConsoleApp1的 .Net Core控制台应用程序,然后添加一些Nuget程序包。
Microsoft.EntityFrameworkCore.Sqlite
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tools
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json
ASP.NET Core 2.1以上的版本中,Microsoft.EntityFrameworkCore.Tools包包含在Microsoft.AspNetCore.App 元包。
而 ASP.NET Core 2.1 以下的版本中需要手动引用 Microsoft.EntityFrameworkCore.Tools包。
2、创建配置文件
新建一个名为appsettings.json
的配置文件,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | { "exclude" : [ "**/bin" , "**/bower_components" , "**/jspm_packages" , "**/node_modules" , "**/obj" , "**/platforms" ], "ConnectionStrings" : { "db" : "Data Source=db/blogging.db" } } |
右击appsettings.json文件属性,修改复制到输出目录的值为“如果较新则复制”。
3、创建模型
新建一个文件夹Models,然后在该文件夹下创建Blog.cs
,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp1.Models { public class Blog { public int BlogId { get ; set ; } public string Url { get ; set ; } public ICollection<Post> Posts { get ; set ; } } } |
在Models文件夹下创建Post.cs
,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp1.Models { public class Post { public int PostId { get ; set ; } public string Title { get ; set ; } public string Content { get ; set ; } public int BlogId { get ; set ; } public Blog Blog { get ; set ; } } } |
最后在同样目录下创建BloggingContext.cs
,内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.IO; using System.Text; namespace ConsoleApp1.Models { public class BloggingContext:DbContext { public DbSet<Blog> Blogs { get ; set ; } public DbSet<Post> Posts { get ; set ; } private IConfiguration configuration; public BloggingContext() { configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile( "appsettings.json" ).Build(); } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite(configuration.GetConnectionString( "db" )); } } } |
4、创建数据库
先创建一个名为db的文件夹,然后打开程序包管理控制台,在控制台下输入以下命令:
1 2 | Add-Migration InitialCreate Update-Database |
创建成功后,会发现db目录下有一个blogging.db数据库。右击blogging.db属性,修改复制到输出目录的值为“如果较新则复制”。
5、使用模型
打开Program.cs,替换成以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using EFCoreSqliteTest.Models; using System; namespace EFCoreSqliteTest { class Program { static void Main( string [] args) { using ( var db = new BloggingContext()) { db.Blogs.Add( new Blog { Url = "https://blog.csdn.net/lms99251" }); var count = db.SaveChanges(); Console.WriteLine( "{0} records saved to database" , count); Console.WriteLine(); Console.WriteLine( "All blogs in database:" ); foreach ( var blog in db.Blogs) { Console.WriteLine( " - {0}" , blog.Url); } Console.ReadKey(); } } } } |
运行程序,控制台窗口中会出现以下内容
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2019-04-04 C#连接Oracle数据库
2019-04-04 OLAP多维数据库备份