简体   繁体   English

连接到 .NET 3.1 中的 Entity Framework Core

[英]Connect to Entity Framework Core in .NET 3.1 Azure Function Project Startup.cs

I am attempting to use entity framework against a database in Azure.我正在尝试对 Azure 中的数据库使用实体框架。 I have a connection string stored in the local.settings.json, but all of my attempts to reference it from a Startup.cs have failed.我在 local.settings.json 中存储了一个连接字符串,但是我从 Startup.cs 引用它的所有尝试都失败了。

I am able to access my connection string using an IConfiguration DI into a function class and I can successfully access the database with SQL Command using the configuration like:我能够使用 IConfiguration DI 访问我的连接字符串到 function class 并且我可以使用 SQL 命令成功访问数据库,使用如下配置:

string connectionString = _configuration.GetConnectionString("cpni");

So I know that the connection string is working and that I can access it.所以我知道连接字符串正在工作并且我可以访问它。

If I try to use DI with IConfiguration on the Startup class , the compiler does not give me any errors, but once it's running in debug I begin getting the following error:如果我尝试在Startup class 上将DI 与 IConfiguration 一起使用,编译器不会给我任何错误,但是一旦它在调试中运行,我就会开始收到以下错误:

System.Private.CoreLib: No parameterless constructor defined for type 'CPNI_Functions.Startup'. System.Private.CoreLib:没有为“CPNI_Functions.Startup”类型定义无参数构造函数。

Here is what I'm currently successfully using with a hardcoded connectionString (since using DI with IConfiguration isn't working):这是我目前成功使用硬编码connectionString 的内容(因为将 DI 与 IConfiguration 一起使用不起作用):

builder.Services.AddDbContext<CpniContext>(
    options => options.UseSqlServer(connectionString));

And that allows me to work with the database through EF.这使我可以通过 EF 使用数据库。 What I would like to use is some combination of that and something like this:我想使用的是它的某种组合和类似的东西:

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");
    });

If that doesn't make sense or won't work, then please let me know what the recommended way of setting this DI up is.如果这没有意义或不起作用,那么请让我知道设置此 DI 的推荐方法是什么。 If possible, I want to avoid using configuration lookup through ConfigurationManager.如果可能,我想避免通过 ConfigurationManager 使用配置查找。

For reference, here is the full Startup.cs and this works:作为参考,这里是完整的 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"));
        }*/
    }
}

I'm new to core and I'm new to Azure Functions, so please forgive my ignorance on this.我是核心新手,我是 Azure 函数的新手,所以请原谅我对此的无知。 Thank you in advance for your help.预先感谢您的帮助。

For an Azure function app you could try adding NuGet package:对于 Azure function 应用程序,您可以尝试添加 NuGet ZEFE90A8E604A7F7640E8Z8D03A

System.Configuration.ConfigurationManager System.Configuration.ConfigurationManager

local.settings.json: local.settings.json:

{
  "ConnectionStrings": {
    "cpni": "[ConnectionStringDetails]"
  }
}

Then hopefully you should be able to access using ConfigurationManager.然后希望您应该能够使用 ConfigurationManager 进行访问。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 StartUp.cs 中为 .net core 3.1 打印进程名称 - How to print process name in StartUp.cs for .net core 3.1 在 ASP.NET Core 3.1 的 Startup.cs 中注入服务 - Inject a service in Startup.cs in ASP.NET Core 3.1 ASP.NET Core中的Startup.cs中kestrel关闭function - Kestrel shutdown function in Startup.cs in ASP.NET Core Startup.cs 从 .net 内核 2.2 迁移到 .net 内核 3.1 - Startup.cs migration from .net core 2.2 to .net core 3.1 如何将 ASP.NET Core 3.1 中 Startup.cs 中的代码移动到 ASP.NET Core 6? - How to move code in Startup.cs in ASP.NET Core 3.1 to ASP.NET Core 6? .net 核心 Startup.cs CreateScope 或 BuildServiceProvider - .net core Startup.cs CreateScope or BuildServiceProvider 如何在登录 .net core 3.1 Web 应用程序时在 startup.cs 中设置全局数据? - How to set global data in startup.cs upon logging in .net core 3.1 web application? 集成测试中的自定义 WebApplicationFactory 未在 Startup.cs 中注册服务(ASP.NET Core 3.1) - Custom WebApplicationFactory in integration testing not registering services in Startup.cs (ASP.NET Core 3.1) 在我的.Net框架应用程序中,“startup.cs”(.Net Core)的等价物是什么? - what is the equivalent of “startup.cs”(.Net Core) in my .Net framework application? .NET Core program.cs 和 Startup.cs 未命中 - .NET Core program.cs and Startup.cs not hit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM