Databases for different clients

4

I am learning MVC 4 and would like to know if there is a way to with only 1 model accessing 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?

    
asked by anonymous 26.05.2014 / 14:54

4 answers

4

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 instance of the system for each client

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

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

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

In your% w / w, set only Connection String :

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

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

Defineanewconfiguration:

Enter the name of your client, for example:

RightclickonyourWeb.configfileandchooseWeb.config:

Notice that a variation of your Add Config Transform file will appear. This is what we call a transformation file :

Openthenewtransformationfile.Notethatthereareafewexamplesthathavebeencommentedon.JustchangetheexamplethathastheConnectionStringtotheConnectionStringofyourclient:

<configurationxmlns: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 Publish using the newly created configuration. In this case, "Client1".

More examples? link

    
26.05.2014 / 17:43
0

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

When the user logs in, he can query the connection string of the user and for the following operations he uses it to connect to the correct DB.

This implies, however, that you have a central DB where you store the information that links the user / DB.

Note: I suggested that you edit your answer and explain to us better the system you have and the purpose to make the question clearer and the answers to be more useful.

    
26.05.2014 / 15:25
-1
Hello, I do not know this MVC 4, but with the experience I have in BD, I would create a table to save the data of all the clients, or would separate it by person, and would classify that person in several ways, such as client, you can also register your clients' addresses and classify them as billing, residential, correspondence, etc, just link using a foreign key as customer_id for example and so it goes .. .

    
26.05.2014 / 15:00
-1

Everything is a matter of business logic,

I suggest you do tables, relationships, and everything you have right.

Because your infrastructure would not have to be as large as if you needed different databases.

    
26.05.2014 / 15:16