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();
}
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!