Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 连接到.NET 3.1 Azure Function Project Startup.cs中的实体框架核心_C#_Azure_Dependency Injection_Entity Framework Core_.net Core 3.1 - Fatal编程技术网

C# 连接到.NET 3.1 Azure Function Project Startup.cs中的实体框架核心

C# 连接到.NET 3.1 Azure Function Project Startup.cs中的实体框架核心,c#,azure,dependency-injection,entity-framework-core,.net-core-3.1,C#,Azure,Dependency Injection,Entity Framework Core,.net Core 3.1,我正试图对Azure中的数据库使用实体框架。我在local.settings.json中存储了一个连接字符串,但是我从Startup.cs引用它的所有尝试都失败了 我可以使用IConfiguration DI将我的连接字符串访问到函数类中,并且我可以使用SQL命令成功访问数据库,配置如下: string connectionString = _configuration.GetConnectionString("cpni"); 所以我知道连接字符串正在工作,我可以访问它 如果我尝试在启动类上使

我正试图对Azure中的数据库使用实体框架。我在local.settings.json中存储了一个连接字符串,但是我从Startup.cs引用它的所有尝试都失败了

我可以使用IConfiguration DI将我的连接字符串访问到函数类中,并且我可以使用SQL命令成功访问数据库,配置如下:

string connectionString = _configuration.GetConnectionString("cpni");
所以我知道连接字符串正在工作,我可以访问它

如果我尝试在启动类上使用DI和IConfiguration,编译器不会给我任何错误,但一旦它在debug中运行,我就会出现以下错误:

System.Private.CoreLib:没有为类型“CPNI_Functions.Startup”定义无参数构造函数

以下是我目前成功使用的硬编码连接字符串(因为将DI与IConfiguration一起使用不起作用):

builder.Services.AddDbContext(
options=>options.UseSqlServer(connectionString));
这使我能够通过EF处理数据库。我想使用的是这两者的某种结合:

builder.Services.AddDbContext<CpniContext>(/*options need to go here?*/)
    .Configure<IConfiguration>((configuration) =>
    {
        //Or are options supposed to go here somewhere, or be bound here with some sort of .Bind()?
        configuration.GetConnectionString("cpni");
    });
builder.Services.AddDbContext(/*选项需要转到此处?*/)
.Configure((配置)=>
{
//或者选项应该放在这里的某个地方,或者用某种.Bind()绑定在这里?
GetConnectionString(“cpni”);
});
如果这没有意义或不起作用,那么请让我知道设置此DI的推荐方法。如果可能,我希望避免通过ConfigurationManager使用配置查找

services.AddDbContext<CpniContext>(
    options => options.UseSqlServer(ConfigurationManager.ConnectionStrings["cpni"].ConnectionString));
以下是完整的Startup.cs,可供参考:

using CPNI_Functions.Data;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

[assembly: FunctionsStartup(typeof(CPNI_Functions.Startup))]

namespace CPNI_Functions
{
    class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            string connectionString = "myconnectionstringhere";

            builder.Services.AddDbContext<CpniContext>(
                options => options.UseSqlServer(connectionString));
        }
        /*
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<CpniContext>(configuration.GetConnectionString("cpni"));
        }*/
    }
}
使用CPNI_函数.Data;
使用Microsoft.Azure.Functions.Extensions.DependencyInjection;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
[汇编:函数启动(类型(CPNI_Functions.Startup))]
名称空间CPNI_函数
{
类启动:函数启动
{
公共覆盖无效配置(IFunctionsHostBuilder)
{
字符串连接string=“myconnectionstringhere”;
builder.Services.AddDbContext(
options=>options.UseSqlServer(connectionString));
}
/*
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(configuration.GetConnectionString(“cpni”);
}*/
}
}

我对core和Azure功能都是新手,所以请原谅我在这方面的无知。提前感谢您的帮助。

对于Azure function应用程序,您可以尝试添加NuGet软件包:

System.Configuration.ConfigurationManager

local.settings.json:

{
  "ConnectionStrings": {
    "cpni": "[ConnectionStringDetails]"
  }
}
然后希望您能够使用ConfigurationManager访问

services.AddDbContext<CpniContext>(
    options => options.UseSqlServer(ConfigurationManager.ConnectionStrings["cpni"].ConnectionString));
services.AddDbContext(
options=>options.UseSqlServer(ConfigurationManager.ConnectionString[“cpni”].ConnectionString));

谢谢您的评论,但这并不能回答我的问题。我的问题是,我无法访问您在操作中使用的配置对象。你能详细说明你是怎么做到的吗?Startup类中的直接注入对我来说不起作用,正如我在原始问题中提到的。当使用Azure Function App时,我更新了,之前的答案是针对web API项目的。希望更好!非常感谢。我确信这是可行的,但我确实希望尽可能避免使用configuration manager。IConfiguration是函数应用程序的一部分,我希望使用它获得一个工作版本。