Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I don't know how use two different databases in the same project using ASP.NET MVC

What I have tried:

I've this structure:
Link

I use two different context.

Firs context, PGDbContext.cs code:
namespace Inspeccions.Models
{
    public class PGDbContext : DbContext
    {
        public PGDbContext() : base( "ActivitatsEntities") { }
        public virtual DbSet<activitats_activitat> Usr { get; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    }
}


ModelInspeccions.Context.cs code:
namespace Inspeccions.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class InspeccionsEntities : DbContext
    {
        public InspeccionsEntities()
            : base("name=InspeccionsEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Criteri> Criteris { get; set; }
        public virtual DbSet<CriterisInspeccio> CriterisInspeccios { get; set; }
        public virtual DbSet<CriterisTipusInspeccio> CriterisTipusInspeccios { get; set; }
        public virtual DbSet<Inspeccio> Inspeccios { get; set; }
        public virtual DbSet<TipusInspeccio> TipusInspeccios { get; set; }
        public virtual DbSet<Usuari> Usuaris { get; set; }
    }
}


Web.config code:
<connectionStrings>
    <add name="ActivitatsEntities" connectionString="Server=***;port=5432;Database=sitsalt;     User Id=***;Password=***" providerName="Npgsql" />
  <add name="InspeccionsEntities" connectionString="metadata=res://*/Models.ModelInspeccions.csdl|res://*/Models.ModelInspeccions.ssdl|res://*/Models.ModelInspeccions.msl;provider=System.Data.SqlClient;provider connection string="data source=SLTPANOLI;initial catalog=Inspeccions;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
  </system.data>


ActivitatsController code:
public ActivitatsController()
{
    _context = new PGDbContext();
}

// GET: Activitats
public ActionResult Index()
{
    return View(_context.Usr.ToList());
}


If I create a project with only one of the connections and context it works, but when I put it in the same project the ModelInspeccions work but the PDGbContext returns a null DbSet...

Can anyone help me please? Thanks!
Posted
Updated 24-Jul-17 22:20pm
Comments
Afzaal Ahmad Zeeshan 25-Jul-17 7:08am    
Okay, but where you are configuring both the databases? Typically, it works just perfect, there is no problem in using multiple databases. You just require to configure which Context goes where, and reads from where.

Also, have a look at the Unit of Work & Repository Pattern for more on this.

1 solution

Hello,

Please follow the below link. For two different databases, you need to have linked servers.

Linked Servers (Database Engine) | Microsoft Docs[^]

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900