Thomas
Thomas

Reputation: 53

How do I access 2 DBContext in one Razor page

In my Razor page I would like to use a second DBContext that is connected to a different database.

I have two DBContext that are connecting to two different database that work fine independently in the same app. DB1 is connected to MS Sql Server running on a Linux box, this is the main database. DB2 is connected to MS Sql Server on a Windows Server 2016. I can create CRUD for tables in DB1 and all functions work correctly, I can create Read for a View in DB2 and data is retrieved as expected. When creating a new record in DB1 I would like to merge data from DB2. How do I create/access a DBContext for DB2 in a Razor page CRUD created for a DBContext for DB1. I hope this makes sense. I have tried for the last couple of days googling like crazy and haven't been able to find a solution.

Upvotes: 0

Views: 4512

Answers (2)

Thomas
Thomas

Reputation: 53

BattlFrog - thank you for you response. It wasn't exactly what I was looking for but it did put me on a better Gooogle Path. How I solved my issue was by the use of "Dependency Injection". I simply added the second DBContext to the contructor of the PageModel. Then in my OnPostAsync() I just had to reference the DBContext. This worked for me, but as I am only learning C#, ASP.Net Core and Razor Pages, this may not be the best approach. Please correct me if I am wrong.

    public class IndexModel : PageModel
    {
        DBContext1 _context1;
        DBContext2 _context2;

        public IndexModel(DBContext1 context1, DBContext2 context2)
        {
            _context1 = context1;
            _context2 = context2;
        }

        public async Task<IActionResult> OnPostAsync()
        {
            IList<ClassName> listName = await _context2.ObjectName.ToListAsync();
            // do some stuff here
            await _context1.SaveChangesAsync();
        }
    }

Upvotes: 5

BattlFrog
BattlFrog

Reputation: 3397

It is best practice to not directly interact with your DbContext in the view, you would make your calls in the Controller then populate a ViewModel with the values from each db, then pass the viewmodel between the view and controller.

So lets say DB1 has our Student data and DB2 has out Teacher data. We need models:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
}


public class Teacher
{
    public string Id { get; set; }
    public string Name { get; set; }
}

public class StudentViewModel
{
    public Student Student { get; set; }
    public Teacher Teacher { get; set; }
}

In our controller we get the data from each DB then combine in the ViewModel:

    var studentData = context1.ModelA.GetStudent(studentId);
    var teacherData = context2.ModelA.GetTeacher(teacherId);

    var viewModel = new StudentViewModel()
    {
        Student = studentData,
        Teacher = teacherData
    };

   return View(viewModel);

In the return of the controller we are returning the viewModel to the view. Then in the view you would do what ever it is you are doing, then post the viewmodel back to the controller to save in the separate Dbs.

View:

@model MyApp.Models.StudentViewModel

<!-- Do stuff -->

Upvotes: 0

Related Questions