如何使用实体框架从多个数据库中检索数据 [英] How do I retrieve data from multiple database using entity framework

查看:84
本文介绍了如何使用实体框架从多个数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架数据模型在C#中开发API。我连接到现有数据库,可以按照我的意愿获取或发布数据。但现在我想获取一些存在于另一个数据库中的数据,并显示来自两个不同数据库的员工信息。

下面是我从单个数据库中检索数据的代码。



我尝试过:



I am developing an API in C#, using entity framework data model. I am connected to an existing database and can get or post data as i wish too. But now i want to fetch some data which is present in another database and display the information of an employee from two different databases.
Below is the code through which i am retrieving data from a single database.

What I have tried:

public HttpResponseMessage Get(int EmployeeId)
{
  var masterInfo = entities.Empmasters.where(e => e.EmployeeId == 
                                             EmployeeId).FirstOrDefault();

  if (masterInfo == null)
  {
     return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No info");
  }
  else
  {
    return Request.CreateResponse(HttpStatusCode.OK, masterInfo);
  }

推荐答案

您可以为另一个数据库创建另一个DBContext和实体。

或者你可以创建新的API项目并调用其端点来获取其他信息。

或者您可以创建一个调用另一个数据库的存储过程,并在当前的DBContext中调用存储过程。
You can create another DBContext and entities for another database.
Or you could create new API project and call its endpoint to get other information.
Or you could create a Stored Procedure that call another database, and call the stored procedure in your current DBContext.


这篇关于如何使用实体框架从多个数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆