问题描述
我不知道如何使用ASP.NET MVC在同一个项目中使用两个不同的数据库
我尝试了什么:
我有这样的结构:
我使用两种不同的背景。
Firs context,PGDbContext.cs代码:
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代码:
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代码:
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代码:
ActivitatsController code:
public ActivitatsController()
{
_context = new PGDbContext();
}
// GET: Activitats
public ActionResult Index()
{
return View(_context.Usr.ToList());
}
如果我创建的项目只有一个连接和上下文可以工作,但当我把它放入同一个项目ModelInspeccions工作,但PDGbContext返回一个null DbSet ......
有人能帮帮我吗?谢谢!
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!
推荐答案
这篇关于两个数据库在同一个项目ASP.NET MVC中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!