更改实体框架DBContext的DB名称

发布于 2025-02-01 16:45:12 字数 334 浏览 4 评论 0原文

我继承了一个使用实体框架访问SQL Server数据库的应用程序。 dbContext类具有构造函数,如下所示,其中building permits是初始目录的名称。

我希望能够通过配置文件在数据库(具有相同连接)之间切换,而不是更改代码。

我该怎么做?

public BuildingPermitsDbContext() : base("BuildingPermits")
{
    Database.SetInitializer<BuildingPermitsDbContext>(null);
}

I have inherited an application which is using Entity Framework to access a SQL Server database. The DbContext class has the constructor as shown below, where BuildingPermits is the name of the initial catalog.

I would like to be able to switch between databases (with the same connection) via a config file instead of changing the code.

How can I accomplish this?

public BuildingPermitsDbContext() : base("BuildingPermits")
{
    Database.SetInitializer<BuildingPermitsDbContext>(null);
}

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

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

发布评论

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

评论(1

新雨望断虹 2025-02-08 16:45:12

您可以尝试一下。

using System.Configuration;

public BuildingPermitsDbContext() : base(DatabaseName())
{
    Database.SetInitializer<BuildingPermitsDbContext>(null);
}

private static string DatabaseName()
{ 
   var db = ConfigurationManager.AppSettingss["desiredDbName"];
   return db;
}

You can try this.

using System.Configuration;

public BuildingPermitsDbContext() : base(DatabaseName())
{
    Database.SetInitializer<BuildingPermitsDbContext>(null);
}

private static string DatabaseName()
{ 
   var db = ConfigurationManager.AppSettingss["desiredDbName"];
   return db;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文