如何连接Somee.com上的ASP.NET Web API for SQL Server数据库?

问题描述 投票:0回答:1

该类型的字符串由站点 SOMEE 发出以连接到 SQL Server:

workstation id=xxx.mssql.somee.com;packet size=4096;user id=xxxx;pwd=xxxx;data source=xxxx.mssql.somee.com;persist security info=False;initial catalog=xxxx;

在本地数据库上,我使用以下连接字符串:

enter image description here

我发布了项目并将其上传到somee.com,但无法建立与远程数据库的连接。如何将

SOMEE-DB-CONNECTION-STRING
转换为普通的 Entity Framework Core 7 连接字符串(如本地连接字符串)?

尝试了一些方法,没有找到解决办法。

c# .net sql-server entity-framework-core connection-string
1个回答
0
投票

要使用 Entity Framework Core 将 ASP.NET Web API 连接到 Somee.com 上的 SQL Server 数据库,您可以按如下方式修改连接字符串:

  1. appsettings.json
    appsettings.Development.json
    文件中的连接字符串替换为您由 Somee.com 提供的连接字符串:
"ConnectionStrings": {
  "DefaultConnection": "workstation id=xxx.mssql.somee.com;packet size=4096;user id=xxxx;pwd=xxxx;data source=xxxx.mssql.somee.com;persist security info=False;initial catalog=xxxx;"
}
  1. 在您的
    Startup.cs
    文件中的
    ConfigureServices
    方法中,将 DbContext 配置为使用此连接字符串:
services.AddDbContext<YourDbContext>(options =>
{
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});

确保将

YourDbContext
替换为 DbContext 的实际名称。

  1. 现在,您可以像平常使用本地数据库一样使用 Entity Framework Core,它将使用您在
    appsettings.json
    中指定的连接字符串连接到 Somee.com 提供的远程 SQL Server 数据库。

请记住将连接字符串中的占位符 (xxx, xxxx) 替换为您的实际数据库凭据。另外,请确保 Somee.com 上的远程 SQL Server 实例允许外部连接,并且防火墙规则已正确配置以允许您的 Web 应用程序连接到它。

© www.soinside.com 2019 - 2024. All rights reserved.