如何从数据库获取连接字符串并在运行时在MVC中连接到它

时间:2017-08-02 18:18:52

标签: c# asp.net-mvc odbc

我是MVC的新手我遇到了一个问题,我的表格上有一个下拉列表,分支名称和每个分支都有一个单独的数据库和用户登录中央数据库一旦用户登录并选择任何分支我想获得连接字符串从数据库中,然后在运行时连接到此连接字符串.. 现在我从数据库table.i获得了连接字符串,像这样

describe('Component: Root Component', () => {

    beforeEach(async(() => {

        TestBed.configureTestingModule({

            declarations: [MyApp],

            providers: [
              { provide: AuthServiceProvider, useClass: AuthServiceProviderMock },
              ConfigProvider,
              StatusBar,
              SplashScreen
            ],

            imports: [
              IonicModule.forRoot(MyApp)
            ]

        }).compileComponents();

    }));

    beforeEach(() => {

        fixture = TestBed.createComponent(MyApp);
        comp    = fixture.componentInstance;

    });

    it('initialises with a root page of LoginPage if not authorized', () => {

        expect(comp['rootPage']).toBe(LoginPage);

    });

});

问题是如何连接到此连接字符串?并从分支数据库中获取数据

1 个答案:

答案 0 :(得分:0)

选项#1: 您可以使用此getting started guide为每个数据库创建实体框架实体模型。这将使访问不同的数据库变得非常容易。

// Access Abcd
using (var db = new AbcdEntities())
{
    return db.Widgets.ToList();
}

// Access Efgh
using (var db = new EfghEntities())
{
    return db.Widgets.ToList();
}

选项2:或者,您可以使用单个连接字符串并在运行时更新连接字符串(see similar post)。假设您的数据库模式完全相同,您仍然可以使用实体框架。