简体   繁体   English

.NET Framework 和 Entity Framework Core:如何获取连接字符串

[英].NET Framework and Entity Framework Core: How can I get hold of the connection string

I've installed EF Core in a .NET Framework 4.7.2 class library.我已经在 .NET Framework 4.7.2 类库中安装了 EF Core。 This is just a project containing some specific functionality, not the StartUp project in the solution.这只是一个包含某些特定功能的项目,而不是解决方案中的StartUp项目。 The project with EF Core is then referenced by the main project, where the web.config and IoC setup lives.然后,带有 EF Core 的项目被主项目引用,其中web.config和 IoC 设置存在。 The solution is for a web site.解决方案是针对网站。

According to this page , .NET Framework 4.7.2 is supported.根据此页面,支持 .NET Framework 4.7.2。

The problem is injecting or otherwise fetching the connection string, or probably any other configuration/appsettings value.问题在于注入或以其他方式获取连接字符串,或者可能是任何其他配置/应用设置值。

Let's say this is my DbContext class:假设这是我的DbContext类:

public class PersonContext : DbContext
{
    public DbSet<Person> Persons { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            if (ConfigurationManager.AppSettings["ConnectionString"] == null)
            {
                throw new Exception("Hey!");
            }

            optionsBuilder.UseSqlServer(ConnectionString);
        }
    }
}

When I run commands like Add-Migration or Remove-Migration , the AppSettings["ConnectionString"] is null, and the exception gets thrown.当我运行Add-MigrationRemove-Migration等命令时, AppSettings["ConnectionString"]为空,并且抛出异常。

I think this is due to the application being in "design mode", and the web.config hasn't been read.我认为这是由于应用程序处于“设计模式”,并且尚未读取web.config I need to be able to specify different connection strings for different environments.我需要能够为不同的环境指定不同的连接字符串。

Any ideas to how I can get the connection string from either the <appSettings> or the <connectionStrings> ?关于如何从<appSettings><connectionStrings>获取连接字符串的任何想法?

Edit: I also want to add that the solution uses Structuremap for IoC, and I can't inject into the DbContext when running the migration commands in the package manager console.编辑:我还想补充一点,该解决方案将 Structuremap 用于 IoC,并且在包管理器控制台中运行迁移命令时,我无法注入 DbContext。

尝试这个:

var connectionString = ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString;

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

相关问题 如何在没有实体框架的情况下获取 .net 核心到 Azure SQL 的连接字符串? - how to get connection string in .net core to Azure SQL without Entity Framework? 如何在 Entity Framework Core 中将连接字符串传递给 DBContext? - How do I pass connection string to DBContext in Entity Framework Core? 使用.net核心时,如何存储实体框架6连接字符串 - How should I store the entity framework 6 connection string when using .net core .NET core - 将连接字符串读入 Entity Framework Core - .NET core - read connection string into Entity Framework Core 实体框架-如何将连接字符串更改为相对? - Entity Framework - how can I change connection string to be relative? 如何关闭实体框架中的连接字符串对象 - How can I close connection string object in Entity Framework ASP.NET核心实体框架空值连接字符串 - ASP.NET core Entity Framework null value connection string 获取实体框架连接字符串 - Get the Entity Framework Connection String 如何将 C# .net 框架项目更改为 .net 核心 3.0 项目? [等候接听] - How can I change a C# .net framework project to a .net core 3.0 project? [on hold] 如何从没有实体框架的.NET Core 2.1 Web API中检索类中的连接字符串 - How to retrieve connection string in class from .NET Core 2.1 Web API w/o Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM