在学习.net core 的认证,但是又不是很想要特地的装一个数据库,于是选用了 sqlite, 打算写一篇 sqlite的记录,以后的我就靠现在的我了
1. 在.net core 3中 entityframework.sqlite也要安装.net core 3的,否则运行时会报错.
2.在appsettings.json设置连接字符串
{
"ConnectionStrings": {
"sqliteConnection": "Data Source=sqlite.db3"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
3. 在Startup.cs的ConfigureServices方法中添加服务
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite($Configuration.GetConnectionString("sqliteConnection")));
services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider, RevalidatingAuthenticationStateProvider<IdentityUser>>();
services.AddSingleton<WeatherForecastService>();
}
4.复制一个空的sqlite数据库文件到项目里,设置较新时复制,生成操作无生成一下项目
5.迁移代码,用包管理器的命令行初始化数据库
Add-Migration Init
7.作者暂时重启一下电脑
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!