简体   繁体   English

从不同的数据库获取数据 ASP.NET MVC

[英]Getting data from different databases ASP.NET MVC

In my ASP.NET MVC application, I want to get data from two contexts and merge it and pass it to the view.在我的 ASP.NET MVC 应用程序中,我想从两个上下文中获取数据并将其合并并传递给视图。

Currently, I did this,目前,我这样做了,

private zSqlLink dbs = new zSqlLink();
private zSqlData db = new zSqlData();

public ActionResult PendingSelfTasks() 
{
    IEnumerable <SelfCareTasks> pendingTasks = new List <SelfCareTasks> ();

    var list = dbs.SelfCareTasks.ToList().Where(x => x.Is_Service_Accepted == false && x.Status == true);

    var TaskList = (
        from d in list join c in db.Customer on d.Customer_Id equals c.Id 
        join ser in db.Services on d.Service_Id equals ser.Id 
        where d.Is_Service_Accepted == false && d.Status == true 
        select new SelfCareTasks 
        {
            Id = d.Id,
            CustomerName = c.Sur_Name + c.Name,
            ServiceName = ser.Service_NameEng,
            ServicePrice = d.Service_Price,
        }).ToList();

    return View(TaskList);
}

What I did was, 1st I created list and get the main records from one context and stored them there, Then I create another list and there I joined the list with the other database values to get the data required.我所做的是,首先我创建了list并从一个上下文中获取主要记录并将它们存储在那里,然后我创建了另一个列表并将该list与其他数据库值连接起来以获取所需的数据。 Here for the list I'm getting the data and when I merge it with another context returns without data.对于list ,我正在获取数据,当我将它与另一个上下文合并时返回没有数据。

Is something I'm doing wrong here?我在这里做错了什么吗?

I'm not sure what your model relations are but as you already know you can't make collective query to different databases.我不确定你的模型关系是什么,但你已经知道你不能对不同的数据库进行集体查询。 You need to make separate queries to each database and map the datа in the code.您需要对每个数据库进行单独查询并在代码中映射数据。 Of course based on the answer of Panagiotis Kanavos you can have other different opinions当然根据 Panagiotis Kanavos 的回答你可以有其他不同的意见

private zSqlLink dbs = new zSqlLink();
private zSqlData db = new zSqlData();

public ActionResult PendingSelfTasks() 
{
    //Get the collections from databases separately 
    var list = dbs.SelfCareTasks.Where(x => x.Is_Service_Accepted == false && x.Status == true).ToList();

    var listIds = list.Select(x => x.Id).Tolist();
    var customer = bd.Customer.Where(x => listIds.Contains(x.Id)).ToList();

    //Them map all you need in the code
    foreach(var selfCareTasks in list)
    {
    }
    
    return View(TaskList);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM