c# - 数据库第一个EF和ASP.NET Core 3 Web API在Azure上发布,连接字符串错误

标签 c# azure asp.net-core-webapi asp.net-core-3.0

我正在开发一个 ASP.NET Core 3 Web API,其中数据库首先生成数据库上下文。 我在 appsettings.json 文件中有连接字符串。

当我在 IIS Express 上本地运行时一切正常。

问题是当我在 Azure 上发布它时,它给出了错误:

System.ArgumentException: Keyword not supported: 'data source'.
[...]

我注意到连接字符串在发布时更改为:

metadata=res://*/DTOs.csdl|res://*/DTOs.ssdl|res://*/DTOs.msl;provider=System.Data.SqlClient;provider connection string='data source=*****;initial catalog=*****;persist security info=True;user id=*****;password=*****;MultipleActiveResultSets=True;App=EntityFramework'",

至:

metadata=res://*/DTOs.csdl|res://*/DTOs.ssdl|res://*/DTOs.msl;provider=System.Data.SqlClient;provider connection string="data source=*****;initial catalog=*****;persist security info=True;user id=*****;password=*****;MultipleActiveResultSets=True;App=EntityFramework""

作为解决办法,我改变了线路

services.AddScoped<palmdtos>(_ => new MyDbContext(Configuration.GetConnectionString("myConnectionString")));

services.AddScoped<palmdtos>(_ => new MyDbContext(Configuration.GetConnectionString("myConnectionString").Replace("&quot;","'").Replace("&amp;", "&")));

有更好的方法吗?

最佳答案

Models created with the EF Designer are different from Code First in that your model already exists and is not generated from code when the application runs. The model typically exists as an EDMX file in your project.

The designer will add an EF connection string to your app.config or web.config file. This connection string is special in that it contains information about how to find the information in your EDMX file.

引用这个article .

原因是我们正在使用的 EDMX 文件之一的连接字符串。由于 EDMX 必须是只读的,因此我们必须在 Azure 中使用不同的连接字符串。

替换"e时;通过单引号 ' ,它会再次正常工作。因此,请转到 azure 网站 > 配置 > 连接字符串 > 使用 自定义 类型添加 conn 字符串。

注意:即使数据库在 Azure 上运行,也请确保为 Entity Framework 连接字符串选择 Custom 而不是 SQLAzure

关于c# - 数据库第一个EF和ASP.NET Core 3 Web API在Azure上发布,连接字符串错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59061094/

相关文章:

c# - Azure - 创建函数应用程序 - 至少一项资源部署操作失败

azure - 从 Azure 应用服务调用 API 时出现 403

asp.net-core - 通过 swagger/postman 以字符串形式发布到 Web api 路径获取 null

c# - 如何停止 .Net Core Web API 中的自引用循环?

c# - 如何手动实例化 Controller 的基于属性的路由?

c# - 正则表达式查找标签之间的文本 C#

c# - 使用 C# 在设备之间传递确认码

c# - 单元测试自定义属性类

c# - 使用各种方法将访问者重定向到网站的不同区域?

c# - 网站在本地运行良好,但在 Windows Azure 上崩溃