简体   繁体   English

如何连接到ASP.NET Core的WebApplication.db数据库?

[英]How can I connect to ASP.NET Core's WebApplication.db database?

I'm experimenting with ASP.NET Core and have created a new Web Application (MVC) site with it: 我正在试验ASP.NET Core,并用它创建了一个新的Web应用程序(MVC)网站:

git init CoreWebApp
cd CoreWebApp
dotnet new -t web
dotnet restore

After the code is bootstrapped I ran the initial EF migration 自举代码后,我进行了初始EF迁移

dotnet ef database update

At this point a database is generated at ./bin/Debug/netcoreapp1.0/WebApplication.db and the site runs and allows users to be registered and login successfully. 此时,将在./bin/Debug/netcoreapp1.0/WebApplication.db生成一个数据库,该站点将运行,并允许用户注册和成功登录。 I'd like to connect to that database via SSMS, LINQPad, or another standard SQL Client, but am having troubles. 我想通过SSMS,LINQPad或其他标准SQL Client连接到该数据库,但是遇到了麻烦。 The "open file" prompts typically look for a .mdf extension and complain about the .db file. “打开文件”提示通常会查找.mdf扩展名,并抱怨.db文件。

How do I connect to an ASP.NET Core, EF Generated WebApplication.db file from a SQL Client? 如何从SQL客户端连接到ASP.NET Core EF生成的WebApplication.db文件?

The dotnet new command is created with portability in mind so it can also run on non-Windows systems. 创建dotnet new命令时要考虑到可移植性,因此它也可以在非Windows系统上运行。 Since there is no SQLExpress for Linux and Mac, the template is created with SQLite as database. 由于没有适用于Linux和Mac的SQLExpress,因此使用SQLite作为数据库创建模板。

So you need an SQLite client to open it. 因此,您需要一个SQLite客户端来打开它。

Of course you're free to replace with any provider which supports EntityFramework Core, like SQLServer. 当然,您可以随意替换为任何支持EntityFramework Core的提供程序,例如SQLServer。

Just replace 只需更换

"Microsoft.EntityFrameworkCore.Sqlite": "1.0.1",

in your project.json with 在你的project.json中

"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",

and

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

with

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

in Startup.cs and finally the connection string in appsettings.json 在Startup.cs中,最后在appsettings.json中连接字符串

"ConnectionStrings": {
  "DefaultConnection": "Data Source=WebApplication.db"
},

with the

"ConnectionStrings": {
  "DefaultConnection": "Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;Trusted_Connection=Yes;"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有实体框架的情况下连接到 ASP.NET Core 中的数据库? - How can I connect to a database in ASP.NET Core without Entity Framework? 如何使用 Visual Studio 2017 将 db2 数据库连接到 ASP.NET 核心 - How to connect db2 database to ASP.NET Core using Visual Studio 2017 .Net Core : 用于连接 DB、DAL、User Secret 和 Asp.Net Core 配置的类库 - .Net Core : Class Library to connect to DB, DAL, User Secret and Asp.Net Core's Configuration 如何在不使用EF的情况下连接ASP.NET Core Web API中的数据库? - How do I connect to database in ASP.NET Core Web API without using EF? 如何在 ubuntu 桌面上使用 Entity Framework Core 将 asp.net core MVC 项目连接到本地数据库 - how to connect asp.net core MVC project to local DB using Entity Framework Core on ubuntu desktop 我可以将 MVC 5 数据库用于 ASP.NET Core 应用程序吗? - Can I use an MVC 5 database for an ASP.NET Core application? 如何将数据库逻辑从我的 Asp.Net MVC 应用程序转移到 ASP.Net Core MVC? - How can I transfer the database logic from my Asp.Net MVC Apllication to ASP.Net Core MVC? 如何使用ASP.NET Core 2.1连接到SignalR集线器 - How can I connect to a SignalR hub using ASP.NET Core 2.1 如何在 C# 中使用 ASP.NET Core 连接外部认证服务器 - How can I connect with an external authentication server with ASP.NET Core in C# 我可以在 ASP.NET Core 的中间件期间访问数据库吗? - Can I access a database during middleware in ASP.NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM