Databases for different customers

I am learning MVC 4 and would like to know if there is a way with only 1 model to access different databases? For example, I have an online system and for each client I have a separate bank. What would be the best practice to do this?

Author: Rvenerosomorici, 2014-05-26

4 answers

Two scenarios:

1. One system, accessing multiple databases

Set a context for each database:

public class Cliente1Context : DbContext
{
    public Cliente1Context() : base("ConnectionStringDoCliente1") {}

    // Definição dos DbSets
    ...
}

public class Cliente2Context : DbContext
{
    public Cliente2Context() : base("ConnectionStringDoCliente2") {}

    // Definição dos DbSets
    ...
}

Web.config for this case

<configuration>
  ...
  <connectionStrings>
    <add name="ConnectionStringDoCliente1" providerName="System.Data.SqlClient" connectionString="Dados da conexão do cliente 1" />
    <add name="ConnectionStringDoCliente2" providerName="System.Data.SqlClient" connectionString="Dados da conexão do cliente 2" />
  </connectionStrings>
  ...
</configuration>

2. One system instance for each client

In this case, it should be a context only with one configuration only:

public class SistemaContext : DbContext
{
    public SistemaContext() : base("DefaultConnection") {}

    // Definição dos DbSets
    ...
}

In your Web.config, set only one Connection String :

<configuration>
  ...
  <connectionStrings>
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Dados da Default Connection" />
  </connectionStrings>
  ...
</configuration>

Set a configuration for each client. This is done by accessing the menu do Configuration Manager :

Access to Configuration Manager

Set a new setting:

New configuration

Enter the name of your customer, for example:

Setting new configuration for client

Right-Click your file Web.config and choose the option Add Config Transform:

Add Config Transform

Notice that a variation of your Web.config file will appear. This is what we call transformation file :

Web.config

Open the new transformation. Notice that inside it has some commented examples. Just change the example that has the Connection String to your client's Connection String:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="DefaultConnection" 
      connectionString="Connection String do Cliente 1" 
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>
  ...
</configuration>

Just do a Build or a Publish using the newly created configuration. In the case, "client1".

More examples? http://go.microsoft.com/fwlink/?LinkId=125889

 4
Author: Leonel Sanches da Silva, 2014-05-26 15:57:32

One solution to your problem is to assign each client a different connection string that points to the database that belongs to them.

When the user logs in, he can query the user's connection string and for the following operations use it to connect to the correct database.

This implies, however, that it has a central database where it stores the information that relates the user / database.

Note: I would suggest that you edit your answer and better explain the system you have and what the purpose for make the question clearer and the answers can be more useful.

 0
Author: Omni, 2014-05-26 13:25:28

Hello, I do not know this MVC 4, but with the experience I have in BD, would create a table to save the data of all customers, or then would separate by person, and would classify this person in various ways, as customer, supplier, user, technician, administrator, etc., as needed, you can also register addresses of your customers and classify them as billing, residential, correspondence, etc., just link using a foreign key as id_cliente for example and so it goes...

 -1
Author: Fabio Branco da Silva, 2014-05-26 13:00:01

Everything is a matter of business logic,

I suggest you make tables, with relationships and everything you are entitled to.

Because your infrastructure wouldn't have to be that big, as if it needed separate databases.

 -1
Author: Edmo, 2014-05-26 13:16:40