【问题标题】:Connection string could not be found running azure function and entity framework找不到运行天蓝色函数和实体框架的连接字符串
【发布时间】:2018-06-03 16:12:02
【问题描述】:

我在一个项目中有一个 Asp.Net MVC 应用程序,我将用于连接到 Entity Framework 的数据库上下文文件移到它自己的项目中,这样我就可以从我的 Asp.Net MVC 应用程序和我的Azure 函数应用。但是在尝试获取连接字符串时运行 Azure Function 会引发异常。

在应用程序配置文件中找不到名为“YogaBandyDatabase”的连接字符串。

我尝试将连接字符串放入 .DataLayer 项目的 app.config 文件中,并尝试将连接字符串放入 local.settings.json 文件中,并尝试将连接字符串放入应用程序设置连接字符串下的门户中但我仍然收到此异常。

问题 - 它在哪里寻找连接字符串,它表示应用程序配置文件?哪一个? .DataLayer 项目中的那个?我是否将其正确放入 app.config 文件中?

这是我的项目结构

这是上下文文件中的错误

这是我在 .DataLayer 项目中的上下文文件

namespace YogaBandy2017.Context
{
public class YogabandyContext : IdentityDbContext<ApplicationUser>
{
    public YogabandyContext() 
        : base("name=YogaBandyDatabase")
    {

    }

    public YogabandyContext(string connectionString)
        : base(connectionString)
    {

    }

    public static YogabandyContext Create()
    {
        return new YogabandyContext();
    }

    public DbSet<YogaBandyError> YogaBandyErrors { get; set; }
    public DbSet<YogaProfile> YogaProfiles { get; set; }
    public DbSet<YogaClass> YogaClasses { get; set; }
    public DbSet<YogaSpaceEvent> YogaSpaceEvents { get; set; }
    public DbSet<ReportImage> ReportImages { get; set; }

    public DbSet<ReportProfile> ReportProfiles { get; set; }

    public DbSet<ReportSpace> ReportSpaces { get; set; }
    public DbSet<YogaSpace> YogaSpaces { get; set; }

    public DbSet<YogaSpaceFollower> YogaSpaceFollower { get; set; }

    public DbSet<YogaProfileFollower> YogaProfileFollower { get; set; }

    public DbSet<RegisteredStudent> RegisteredStudents { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        base.OnModelCreating(modelBuilder);
    }
}
}

这是带有连接字符串的local.settings.json文件(可能语法不正确)

{
 "IsEncrypted": false,
  "Values": {
   "AzureWebJobsStorage": "the string",
   "AzureWebJobsDashboard": "",
   "yogabandy2017_RootManageSharedAccessKey_SERVICEBUS": "the string"
 },
 "ConnectionStrings": {
   "YogaBandyDatabase": "the string"
 }
}

这是 .DataLayer 项目的 app.config 文件中的连接字符串

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="YogaBandyDatabase" connectionString="my string" providerName="System.Data.SqlClient" />
    <add name="YogaBandyErrorDatabase" connectionString="my string" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v13.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

【问题讨论】:

  • 你有想过这个吗?我现在的情况和你一样。

标签: entity-framework azure connection-string azure-functions


【解决方案1】:

这个有点棘手。登录 Azure 门户并导航到您的函数。单击您的函数的概览选项卡上的应用程序设置链接。将您的 EF 连接字符串放在连接字符串部分下。

【讨论】:

  • 好笑!我不太确定我是否关心它在门户中的工作,我更想知道如何在不将其粘贴到门户中的情况下让它工作。因为我正在使用 Visual Studio 并将其从 VS 发布到 Azure,所以我想维护 VS 中的所有内容!
  • 仅供参考 - 我确实这样做了,但我仍然收到同样的异常
  • @user1186050,我在这个问题上是一招一式。我能想到的就这些了。
猜你喜欢
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多