带有EF模型设计器的单独配置文件中的连接字符串

问题描述 投票:1回答:1

[当我尝试从数据库中更新模型时,如何在设计时让EF模型设计人员在单独的配置文件中看到连接字符串,而不提示'选择数据连接'。

我有一个单独的配置文件,用于在不同环境下运行的连接字符串。在app.config中,我使用<connectionStrings configSource="connections.config">

我不想将连接保存在app.config或web.config中。运行时效果很好,似乎对设计人员来说是一个很大的限制。

要重现该问题,只需创建一个新的ADO.net实体数据模型。将连接字符串存储在app.config中。您将获得如下所示的应用程序fonfig

<?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>
   <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
   </startup>
   <entityFramework>
     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
     <providers>
       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
     </providers>
  </entityFramework>
  <connectionStrings>
    <add name="Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=myserver;initial catalog=Devdb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
 </configuration>

现在复制连接字符串并将其放入新文件'myconnections.config'

<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
  <add name="Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=myserver;initial catalog=Devdb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

编辑app.config并将连接字符串设置更改为

<connectionStrings configSource="myconnections.config">

模型设计者现在将不知道该连接。如果单击“从数据库更新模型”,它将提示您建立连接,并希望将连接保存回app.config。真令人沮丧。

c# entity-framework
1个回答
0
投票

我知道这个问题已有5年历史了,但实际上我找到了解决方案。我正在使用VS2019,并且在Server Explorer中添加新的数据连接时,EF将看到我的新连接。尽管这实际上不是完美的解决方案,因为我们必须单独添加连接字符串,但它仍然可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.