[英]Connection string for using SQLite with ASP.Net MVC5
我正在做一个需要与数据库交互的项目。 因此,我想在我的 ASP.NET MVC 5 项目中使用 SQLite。
我发现一些博客和网站展示了从 ASP.NET MVC 附加 SQLite 所需的一些连接字符串,但这些都是非常老式的(大约 6 岁)。
所以,我的问题是:在web.config
需要什么连接字符串才能将 SQLite 数据库文件附加到我的 ASP.NET MVC 5 应用程序?
另外,将 SQLite 与 ASP.NET MVC 5 应用程序连接的先决条件是什么?
对于asp.net core 2.0
。 对todo
web-api 示例https://docs.microsoft.com/aspnet/core/tutorials/first-web-api#create-the-project 的修改。 一个彻底的描述,会很大,但应该包含要完成的主要项目。
*. 在文件appsettings.json
添加条目
"ConnectionStrings": {
"DefaultConnection": "Data Source=mydb.sqlite"
}
*. 在Startup.cs
修改方法ConfigureServices(IServiceCollection services)
:
public void ConfigureServices(IServiceCollection services)
{
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
string ConnectionStr = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<TodoContext>(options => options.UseSqlite(ConnectionStr));
SQLitePCL.Batteries.Init();
services.AddMvc();
}
*. 在Startup.cs
部分using
添加
using Microsoft.Data.Sqlite;
using SQLitePCL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.