[英]How to connect to existing database? .NET Core API & SQL Server 2012
Good day! 美好的一天!
I am struggling on how to connect existing database with existing data to my API project. 我正在努力将现有数据库与现有数据连接到我的API项目。 And also how do I know if I'm already connected?
还有如何知道我是否已经连接? I am using .NET Core 1.1 and my database is SQL Server 2012.
我正在使用.NET Core 1.1,而我的数据库是SQL Server 2012。
appsettings.json : appsettings.json:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=.\\SQLEXPRESS;Database=;User Id=sa;Password=Passw0rd;MultipleActiveResultSets=True"
}
}
}
Startup.cs - ConfigureServices Startup.cs-ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddDbContext<DefaultConnection>(
options => options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
}
I am following this tutorial but it seems that this creates new database. 我正在关注本教程,但看来这会创建新的数据库。 http://www.mithunvp.com/aspnet-core-web-api-entity-framework-core/
http://www.mithunvp.com/aspnet-core-web-api-entity-framework-core/
Would highly appreciate all your comments/solutions/suggestions. 非常感谢您的所有评论/解决方案/建议。
Thank you very much! 非常感谢你!
You need to add the name of the database into that line : 您需要在该行中添加数据库名称:
"DefaultConnection": "Server=COGSOL-LAP13;Database=DATABASENAME;UserId=sa;Password=Passw0rd;MultipleActiveResultSets=True"
And as CodeCaster comment says, you should use the name of the server too 正如CodeCaster的评论所说,您也应该使用服务器的名称
You also have a space between User and Id User Id
should be 用户和ID之间也应有一个空格
User Id
应该为
UserId
用户身份
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.