实体框架数据库首先和动态连接字符串为100多个SQL Server数据库 [英] Entity Framework database first and dynamic connection string for 100+ SQL Server databases

查看:136
本文介绍了实体框架数据库首先和动态连接字符串为100多个SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Entity Framework的新手,想知道如何修改代码中的连接字符串。

I am new to Entity Framework and want to know how to modify the connection string in code.

这是我的代码

using (var context = new PushjobModel.PushjobEntities())
{
    var list = context.GetAllActivePushjobs();

    foreach (PushjobModel.Pushjob item in list)
    {
        item.IsActive = false;
        item.IsSent = true;
    }

    context.SaveChanges();
}

我的ASP.NET Webforms应用程序中有一个配置文件,其中包含一个列表的连接字符串为100多个数据库。之前我调用 context.GetAllActivePushjobs(); 我想更改连接字符串。

I have a configuration file in my ASP.NET webforms application which contains a list of connection strings for 100+ databases. Before I call context.GetAllActivePushjobs(); I want to change the connection string.

使用旧的 ADO.NET ,我一直使用以下方法很长时间,它的工作完美

With old ADO.NET I have been using following approach for long time and it works perfectly

spPremisesReadTableAdapter taPremises = new spPremisesReadTableAdapter();

taPremises.Connection.ConnectionString = GetConnectionString(CompanyName);

taPremises.GetData();
...

public static void GetConnectionString(string CompanyName)
{
    string connectionstring = System.Web.Configuration.WebConfigurationManager.ConnectionStrings[CompanyName].ConnectionString;
}

任何人都可以帮助?

推荐答案

您只需要将连接字符串条目的名称提供给您的 DbContext 的构造函数:

You just need to provide the name of the connection string entry to your constructor of your DbContext:

using (var context = new PushjobModel.PushjobEntities(name-of-connection-string-here))
{
   .....
}

这篇关于实体框架数据库首先和动态连接字符串为100多个SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆