Change Database during runtime in Entity Framework, without changing the Connection

前端 未结 6 1657
一生所求
一生所求 2020-12-14 01:59

I have a server that hosts 50 databases with identical schemas, and I want to start using Entity Framework in our next version.

I don\'t need a new connection for ea

6条回答
  •  Happy的楠姐
    2020-12-14 02:55

    I have implemented this in my current project in which we have a common security database and different database for every client in the project. So our security database has a table that contain connection string for every other database. We just pass client id and get the connection string of the client database..

    For this add two EDMX one for the common database and other for common schema databases. When user login or what might be your scenario to choose database go to common databse and get the connection string and create object of the needed database. Here is Code sample any, if any quer let me know..

    You can keep connection string regarding every other database in a table in a a common database shared by all the other database.

    EntityInstance_ReviewEntities.GetContext(GetConnectionString(ClientId));
    
    
    private string GetConnectionString(int TenantId)
            {
                EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
                ISecurityRepository objSecurity = new SecurityRepository();
                string tenantConnectionString = objSecurity.GetClientConnectionString(TenantId);
                entityBuilder.ProviderConnectionString = tenantConnectionString;
                entityBuilder.Provider = "System.Data.SqlClient";
                entityBuilder.Metadata = @"res://*/ClientEntity.YourEntity.csdl|res://*/ClientEntity.ADBClientEntity.ssdl|res://*/ClientEntity.YourEntity.msl";
                return entityBuilder.ToString();
            }
    

提交回复
热议问题