Databases for different customers

Asked

Viewed 1,399 times

4

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

  • Let me see if I understand, it is 1 system (site) and that several clients will access the same URL but based on the customer has to change the bank.

  • 2

    Do you have any plausible reason for this? I don’t understand your need...

  • Caro Rvenerosomorici, vi como tag Entity Framework ORM, and then your question says nothing, could improve your question?

  • Paulohdsousa, that would be it. Ronny Amarante, is for learning.

  • Harry Potter, MVC4 works with the Entity Framework, and my question is just this if with 1 model in the project I can work with several banks.

4 answers

4

Two scenarios:

1. A system, accessing several 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. A system instance 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 his Web.config, define 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 of Configuration Manager:

Acesso ao Configuration Manager

Set a new configuration:

Nova configuração

Type your client name, for example:

Definindo nova configuração para cliente

Right click on your file Web.config and choose the option Add Config Transform:

Add Config Transform

Note that a variation of your file will appear Web.config. That’s what we call transformation file:

Web.config

Open the new transformation file. Note that inside it you have some commented examples. Just change the example you have to Connection String to the Connection String of your client:

<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 one now Build or a Publish using the new configuration created. In this case, "Customer1".

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

  • 1

    Thank you very much Gypsy, today I will be, tomorrow I put if it worked.

0

A solution to your problem is to assign each customer one connection string that points to the comic book that belongs to him.

When the user enters, you can refer to connection string user and for the following operations use it to connect to the correct BD.

This implies however that you have a central BD where you store the user/BD related information.

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

  • Omni, it’s just learning, I don’t have a specific system, I just want to learn how to do with only 1 model in the project working with variable banks according to the client

  • In this case then I would say that the form I described can help you. However the answer assumes that all Bds share the same structure.

-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 customers, or then separate by person, and 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 cobranca, residential, correspondence, etc., just link using a foreign key as id_client for example and so will...

-1

It’s all about business logic,

I suggest you make tables, with relationships and everything that is right.

Because your infrastructure wouldn’t need to be as big as if you needed separate databases.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.