gpt4 book ai didi

entity-framework - Azure函数与数据库的连接字符串使用 Entity Framework 的第一种方法

转载 作者:行者123 更新时间:2023-12-02 06:47:22 24 4
gpt4 key购买 nike

我有一个使用数据库优先方法连接字符串的 Azure 函数。连接字符串位于 local.settings.json 文件中,其中包含敏感数据。

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true"
},
"ConnectionStrings": {
"MyConnectionString": "data source=localhost\\sqlexpress;initial catalog=SampleQrCodes;user id=sa;password=****;MultipleActiveResultSets=True;App=EntityFramework"
}
}

如果我尝试删除providerName,则会出现以下异常:

The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly.....you pass it to one of the base DbContext constructors that take a DbConnection

实际连接字符串(从 EF 生成):

<add name="DataContext" connectionString="metadata=res://*/EFModel.csdl|res://*/EFModel.ssdl|res://*/EFModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\sqlexpress;initial catalog=SampleQrCodes;user id=sa;password=***;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

这是类似的问题Load Connection String from Config File in Azure Functions但它对我不起作用如何通过数据库优先方法在 local.settings.json 中定义此连接字符串?

最佳答案

根据你的描述,这里有类似的git hub issue 。根据我的测试,您可以在 local.settings.json 文件中配置连接字符串,如下所示:

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account-key>",
"AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account-key>"
},
"ConnectionStrings": {
"DataContext": {
"ConnectionString": "metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=<server-name>.database.windows.net;initial catalog=<database-name>;persist security info=True;user id=<username>;password=<password>;MultipleActiveResultSets=True;App=EntityFramework'",
"ProviderName": "System.Data.EntityClient"
}
}
}

为了部署到 Azure 函数,您可能需要修改 DbContext。更多详情,可以引用这个类似的issue .

关于entity-framework - Azure函数与数据库的连接字符串使用 Entity Framework 的第一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46885319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com