Entity framework 在ASP.NET核心Windows服务项目中解析EFv6.3中的连接字符串
我有一个ASP.NET核心应用程序作为Windows服务运行。由于项目需求,我使用的是EntityFrameworkV6.3(而不是EFCore) 在执行迁移以及将服务发布到服务器时,检索正确的连接字符串时遇到问题。在本地运行服务时,将成功检索连接字符串 据我所知,EntityFramework6应该从web.config或app.config文件检索连接字符串,对吗?因此,我有一个app.config文件,其中包含两个连接字符串,例如Entity framework 在ASP.NET核心Windows服务项目中解析EFv6.3中的连接字符串,entity-framework,asp.net-core,entity-framework-6,Entity Framework,Asp.net Core,Entity Framework 6,我有一个ASP.NET核心应用程序作为Windows服务运行。由于项目需求,我使用的是EntityFrameworkV6.3(而不是EFCore) 在执行迁移以及将服务发布到服务器时,检索正确的连接字符串时遇到问题。在本地运行服务时,将成功检索连接字符串 据我所知,EntityFramework6应该从web.config或app.config文件检索连接字符串,对吗?因此,我有一个app.config文件,其中包含两个连接字符串,例如 <connectionStrings>
<connectionStrings>
<add name="DataContext" providerName="System.Data.SqlClient" connectionString="Server=localhost\SQLEXPRESS;[redacted]" />
<add name="CrmContext" providerName="System.Data.SqlClient" connectionString="Server=localhost\SQLEXPRESS;[redacted]" />
</connectionStrings>
当我这样做时,它会创建并更新LocalDB数据库,而不是使用我的连接字符串
我还尝试显式引用连接字符串:
update-database -ConnectionStringName "CrmContext"
这将导致以下错误:
在中找不到名为“CrmContext”的连接字符串
应用程序配置文件
我的CrmContext类存在于我的ASP.NET核心windows应用程序中,其中我的DataContext类存在于单独的“数据”项目中(因为它与ASP.NET MVC v5应用程序共享)
当我发布我的服务并将应用程序作为Windows服务安装时,我发现它也根本没有从任何配置文件中获取连接字符串-它再次只是查找默认的localdb数据库。据我所知,它应该从我的PaymentBatchProcessorService.exe.config文件中获取,对吗
我的PaymentBatchProcessorService.exe.config文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<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="DataContext" providerName="System.Data.SqlClient" connectionString="redacted" />
<add name="CrmContext" providerName="System.Data.SqlClient" connectionString="redacted" />
</connectionStrings>
</configuration>
e、 在上面的示例中,工作目录路径解析为my.exe用于Windows服务的位置
谢谢你读到这里 部署服务时,.config文件应改为servicename.exe.config。它应该位于使用installutil注册服务的同一文件夹中。请参阅。谢谢,我尝试了一下,将app.config重命名为PaymentBatchProcessorService.exe.config,并仔细检查了它的内容,但不幸的是,它仍然没有找到连接字符串。它给出了错误:“System.InvalidOperationException:在应用程序配置文件中找不到名为“DataContext”的连接字符串。”这对我来说很奇怪,因为它肯定在同一个文件夹中。参见我发布的另一个问题。当我发布它时,它会生成一个“app.config”文件。当我按照您的建议重命名该文件以包含服务名称时,由于某些原因,它仍然无法获取该名称。我知道,当我用.NET Framework发布命令行程序时,它会正确地用exe名称命名配置文件(例如JobRunner.exe.config)。我不知道为什么它不能为我的.NET核心服务执行此操作。是的,我想您可以传递从ConfigurationManager获得的连接字符串。
internal sealed class CrmConfiguration : DbMigrationsConfiguration<CrmContext>
{
public CrmConfiguration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(CrmContext context)
{
...
context.SaveChanges();
}
}
update-database -ConfigurationTypeName CrmContext
update-database -ConnectionStringName "CrmContext"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<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="DataContext" providerName="System.Data.SqlClient" connectionString="redacted" />
<add name="CrmContext" providerName="System.Data.SqlClient" connectionString="redacted" />
</connectionStrings>
</configuration>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureAppConfiguration((hostingContext, config) =>
{
var workingDirectoryPath = Environment.GetEnvironmentVariable(EnvironmentVariables.ServiceWorkingDirectory);
config.AddXmlFile(Path.Combine(workingDirectoryPath, "app.config"));
config.AddXmlFile(Path.Combine(workingDirectoryPath, "web.config"));
})