单个域模型中的MVC多个数据库

时间:2012-05-01 13:37:18

标签: asp.net-mvc multiple-databases domain-model

我正在编写一个基于Steve Sanderson的Pro Asp .Net MVC框架的应用程序,该框架使用类似于Sports Book app上描述的数据建模。我的应用程序运行良好,我使用Castle项目作为IOC,但我有两个数据库。第一个存储了许多类型的数据和我的客户信息。第二个仅存储每个客户的医疗数据。我需要创建一个类,它返回位于数据库1上的clients表中的数据和存储在数据库2上的每个客户端的医疗信息。

以下是我的代码:

我在web.config上的连接字符串:

<add name="CNRConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=CNR;Persist Security Info=True;User ID=sa;Password=@#@@@!" providerName="System.Data.SqlClient"/>

<add name="CNRpeConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=pe;Persist Security Info=True;User ID=sa;Password=@#@@@!" providerName="System.Data.SqlClient"/>

我的新课程(只是数据,没有表格连接):

namespace DomainModel.Entities
{
    public class ImprimePeriodoPaciente
    {
        public int CodConsulta { get; set; }
        public DateTime DataConsulta { get; set; }
        public string Evolucao { get; set; }
        public int CodProfissional1 { get; set; }
        public int CodPaciente1 { get; set; }
        public string NomeProfissional { get; set; } //receives data from other db
        public string NomePaciente { get; set; } //receives data from other db
        public int CodProcedimento { get; set; }
        public bool TagConsulta { get; set; }
        public int? TagConsulta2 { get; set; } 
        public DateTime di { get; set; }
        public DateTime df { get; set; }
    }
}

这是我从domainmodel的具体和界面:

public  class SqlConsultasRepository:IConsultasRepository
    {
     private Table<Consulta> consultasTabela;
     private Table<TEvolucao> tEvolucaoTabela;
     private Table<Paciente> pacientesTabela;
     private Table<Funcionario> funcionariosTabela;

     public SqlConsultasRepository(string connectionString)
     {
         consultasTabela = (new DataContext(connectionString)).GetTable<Consulta>();
         tEvolucaoTabela = (new DataContext(connectionString)).GetTable<TEvolucao>();
         pacientesTabela = (new DataContext(connectionString)).GetTable<Paciente>();
         funcionariosTabela = (new DataContext(connectionString)).GetTable<Funcionario>();
     }


        public IQueryable<ImprimePeriodoPaciente> Prontuarios            { 
            get
            { 
                return
                    (
                       from c in consultasTabela
                       join p in pacientesTabela on c.CodPaciente1 equals p.CodigoPaciente //my doom
                       join f in funcionariosTabela on c.CodProfissional1 equals f.CodigoFuncionario //my doom
                       select new ImprimePeriodoPaciente
                       {
                           CodConsulta=c.CodConsulta,
                           DataConsulta=c.DataConsulta,
                           Evolucao=c.Evolucao,
                           NomeProfissional= f.NomeFuncionario, //my doom
                           NomePaciente=p.NomePaciente, //my doom
                           CodProfissional1=c.CodProfissional1,
                           CodPaciente1 = c.CodPaciente1
                       }
                   );
            }

        }

代码标有'my doom'以显示问题发生的位置....我需要创建一个对其他数据库的connectionString引用,因为这些表不在connectionString变量指定的当前数据库中。我收到错误,说数据不是来自相同的datacontext(或类似的东西)。

那么,你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

根据这篇文章了解了它:

http://dotnetprogrammingtipsbymuhil.blogspot.com.br/2009/12/query-contains-references-to-items.html

对于遇到同样问题的人,请按照上述后续步骤进行操作。

我的项目我已经创建了一个具体方法的接口,该方法从每个数据库中获取分离的数据并将它们放在单独的列表中,如下所示:

namespace DomainModel.Concrete
{
    public class SqlImprimePeriodoPacientesRepository : IImprimePeriodoPacientesRepository
    {
    private static string cConexao = "Data Source=.\\SQLEXPRESS;Initial Catalog=CNR;Persist Security Info=True;User ID=sa;Password=23092000";
    private static string cConexao2 = "Data Source=.\\SQLEXPRESS;Initial Catalog=PE;Persist Security Info=True;User ID=sa;Password=23092000";

    private IConsultasRepository cRepository = new SqlConsultasRepository(cConexao2);
    private IPacientesRepository pRepisotory = new SqlPacientesRepository(cConexao);
    private IFuncionariosRepository fRepository = new SqlFuncionariosRepository(cConexao);

    public IQueryable<ImprimePeriodoPaciente> ImprimePeriodoProntuarios//(int? codpac, DateTime? di, DateTime? df)
    {
        get
        {
            var consultas =
                (
                    from c in cRepository.Consultas
                    select new
                    {
                        c.cdf,
                        c.cdi,
                        c.CodConsulta,
                        c.CodPaciente1,
                        c.CodProcedimento,
                        c.CodProfissional1,
                        c.DataConsulta,
                        c.df,
                        c.di,
                        c.Evolucao,
                        c.TagConsulta,
                        c.TagConsulta2
                    }
                )
                .ToList();

            var pacientes =
                (
                    from p in pRepisotory.Pacientes
                    select new
                    {
                        p.CodigoPaciente,
                        p.NomePaciente
                    }
                )
                .ToList();

            var funcionarios =
                (
                    from f in fRepository.Funcionarios
                    select new
                    {
                        f.CodigoFuncionario,
                        f.NomeFuncionario
                    }
                )
                .ToList();

            var result =
                (
                    from c in consultas
                    join p in pacientes on c.CodPaciente1 equals p.CodigoPaciente
                    join f in funcionarios on c.CodProfissional1 equals f.CodigoFuncionario
                    select new ImprimePeriodoPaciente
                    {
                        CodConsulta = c.CodConsulta,
                        DataConsulta = c.DataConsulta,
                        Evolucao = c.Evolucao,
                        NomeProfissional = f.NomeFuncionario,
                        NomePaciente = p.NomePaciente,
                        CodProfissional1 = c.CodProfissional1,
                        CodPaciente1 = c.CodPaciente1
                    }
                ).ToList();


            return result.AsQueryable();
        }
    }
}
}

该方法在Domain的模型方面完成了工作,所以其余方法都在于处理结果。

希望这能有所帮助!