创建数据库
创建文件夹
./Context
创建文件
./Context/BaseEnity.cs
./Context/Memo.cs
./Context/MyTodoContext.cs
./Context/Todo.cs
./Context/User.cs
创建数据对象
-
./Context/BaseEnity.cs
namespace MyToDo.Api.Context { public class BaseEnity { public int Id { get; set; } public DateTime CreateDate { get; set; } public DateTime UpdateDate { get; set; } } }
-
./Context/Memo.cs
namespace MyToDo.Api.Context { public class Memo:BaseEnity { public string Title { get; set; } public string Content { get; set; } } }
-
./Context/MyTodoContext.cs
创建数据库DbSet
using Microsoft.EntityFrameworkCore; namespace MyToDo.Api.Context { public class MyTodoContext:DbContext { public MyTodoContext(DbContextOptions<MyTodoContext> options) : base(options) { } public DbSet<Todo> TodoSet { get; set; } public DbSet<User> UserSet { get; set; } public DbSet<Memo> MemoSet { get; set; } } }
-
./Context/Todo.cs
namespace MyToDo.Api.Context { public class Todo:BaseEnity { public string Title { get; set; } public string Content { get; set; } public int Status { get; set; } } }
-
./Context/User.cs
namespace MyToDo.Api.Context { public class User:BaseEnity { public string Account { get; set; } public string UserName { get; set; } public string Password { get; set; } } }
添加nuget包
-
Microsoft.EntityFrameworkCore.Design
Shared design-time components for Entity Framework Core tools.
-
Microsoft.EntityFrameworkCore.Sqlite
SQLite database provider for Entity Framework Core.
-
Microsoft.EntityFrameworkCore.Tools
Entity Framework Core Tools for the NuGet Package Manager Console in Visual Studio.
Enables these commonly used commands:
Add-Migration
Bundle-Migration
Drop-Database
Get-DbContext
Get-Migration
Optimize-DbContext
Remove-Migration
Scaffold-DbContext
Script-Migration
Update-Database
配置连接字符串
- appsettings.json
{
"ConnectionStrings": {
"TodoConnection": "Data Source=todo.db"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
配置数据库连接选项
- program.cs
builder.Services.AddDbContext<MyTodoContext>(option =>
{
var constr = builder.Configuration.GetSection("ConnectionStrings")["TodoConnection"];
option.UseSqlite(constr);
});
var app = builder.Build();
启动配置数据库
[工具]-> [Nuget包管理器]-> [程序包管理器控制台]
-
输入 :Add-Migration mytodo
生成数据库配置项文件
-
输入 :Update-Database文章来源:https://www.toymoban.com/news/detail-604767.html
将数据库更新到sqlite数据库中,制成sqlite数据库文件文章来源地址https://www.toymoban.com/news/detail-604767.html
到了这里,关于WPF实战学习笔记08-创建数据库的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!