0

我正在编写一个基于 Steve Sanderson 的 Pro Asp .Net MVC 框架的应用程序,它使用了该书中 Sports Store 应用程序中描述的数据建模。我的应用运行良好,我使用 Castle 项目作为 IOC,但我有两个数据库。第一个存储多种数据和我的客户信息。第二个只存储每个客户的医疗数据。我需要创建一个类,它返回位于数据库 1 上的客户表中的数据以及存储在数据库 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
                       }
                   );
            }

        }

该代码标有“我的厄运”以显示问题发生的位置......我需要创建对另一个数据库的 connectionString 引用,因为此表不在 connectionString 变量指定的当前数据库中。我收到错误消息,说数据不是来自相同的数据上下文(或类似的东西)。

那么,你能帮忙吗?

4

1 回答 1

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

该方法在域的模型方面完成了工作,所以剩下的就是处理结果。

希望这对某人有帮助!

于 2012-05-02T02:10:45.200 回答