ASP.NET MVC应用程序中的两个上下文

时间:2017-07-18 12:23:50

标签: asp.net-mvc-5 dbcontext multiple-databases

我有这个结构:

enter image description here

PGDbContext.cs代码:

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代码:

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代码:

 <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=&quot;data source=SLTPANOLI;initial catalog=Inspeccions;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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代码:

public class ActivitatsController:Controller {     PGD​​bContext _context;

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

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

如果我创建的项目只有一个连接和上下文可以工作,但是当我把它放在同一个项目中时,ModelInspeccions可以工作,但是PDGbContext返回一个空的DbSet ...

有人能帮帮我吗?谢谢!

0 个答案:

没有答案