在学习.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.作者暂时重启一下电脑
原文地址:https://www.cnblogs.com/nocanstillbb/p/11442561.html
时间: 2024-10-10 18:57:05