实体框架中的连接字符串

发布于 2025-01-25 05:25:12 字数 1056 浏览 2 评论 0 原文

我使用实体框架在ASP.NET中引用了2个数据库。

在我的 web.config 文件中,我可以看到2个数据库的连接字符串:

<connectionStrings>
    <add name="RContext" 
         connectionString="metadata=res://*/Models.RModel.csdl|res://*/Models.RModel.ssdl|res://*/Models.RModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\SQLEXPRESS;initial catalog=RStreamline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
    <add name="CEntities" 
         connectionString="metadata=res://*/Models.CModel.csdl|res://*/Models.CModel.ssdl|res://*/Models.CModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\SQLEXPRESS;initial catalog=RStreamline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
</connectionStrings>

我可以以某种方式实现替代连接字符串,该连接字符串在其中数据源引用prod Server以供发布?

I am referencing 2 databases in ASP.NET using Entity Framework.

In my web.config file, I can see the connection strings for the 2 databases:

<connectionStrings>
    <add name="RContext" 
         connectionString="metadata=res://*/Models.RModel.csdl|res://*/Models.RModel.ssdl|res://*/Models.RModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\SQLEXPRESS;initial catalog=RStreamline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 
         providerName="System.Data.EntityClient" />
    <add name="CEntities" 
         connectionString="metadata=res://*/Models.CModel.csdl|res://*/Models.CModel.ssdl|res://*/Models.CModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\SQLEXPRESS;initial catalog=RStreamline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 
         providerName="System.Data.EntityClient" />
</connectionStrings>

Can I somehow implement alternate connection strings where the datasource refers to the prod server for the release?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北笙凉宸 2025-02-01 05:25:12

通常用

在您的项目中,您将拥有:

  • web.config
  • web.release.config,

例如在您的web.release.config transform中,您会拥有类似的东西:

<?xml version="1.0"?>
<configuration xmlns:xdt="https://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="RContext" 
      connectionString="RContext-Prod-Connection-String" 
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    <add name="CEntities" 
      connectionString="CEntities-Prod-Connection-String" 
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>
</configuration>

您会注意到 xdt:transform =“ setAttributes” xdt:loacator:locator =“ Match(name)” 位,其中说,在主web.config中,通过名称找到连接串,然后用此处定义的属性替换其属性。

当您发布应用程序时,这将自动发生。

This is typically handled with web.config transforms.

In your project you would have:

  • web.config
  • web.Release.config

For example in your web.Release.config transform you would have something like this:

<?xml version="1.0"?>
<configuration xmlns:xdt="https://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="RContext" 
      connectionString="RContext-Prod-Connection-String" 
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    <add name="CEntities" 
      connectionString="CEntities-Prod-Connection-String" 
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>
</configuration>

You'll notice the xdt:Transform="SetAttributes" xdt:Locator="Match(name)" bit, which says, in the main web.config find the connectionString by name and replace its attributes with the ones defined here.

This will automatically happen when you publish the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文