Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

How do I use Multiple DBContext in my Controller Class

$
0
0

I have a controller class called SearchController.cs

I have a DBContext call to my a SQL Database inside the controller:

namespace Rescue.Exchange.API.Controllers
{
    [AllowAnonymous]
    //This controller will Post all PCR data from the NEMSISv3 database to the Resuce Exchange landing page. 
    [Route("api/[controller]")]
    [ApiController]
    public class SearchController : UserIdentityController
    {

        private readonly NEMSISv3Context _context;

        public SearchController(NEMSISv3Context context)
        {
            _context = context;

                } 




//Do Something with the database call _context

//I want to do something with my second database call, I guess _contextLog? } }

How can I use another DBContext inside the same controller to call to a separate database?

I have the following DBContext in my startup:

services.AddDbContext<NEMSISv3Context>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("SearchConnection")));
            services.AddDbContext<ExchangeContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("RescueExchangeConnection")));

If someone could please show me how the code would work that would be great.  I am fairly new to C# and .NET Core.


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>