我正在开发一个 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(""","'").Replace("&", "&")));
有更好的方法吗?
最佳答案
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/