0

How can I use two database contexts in an ASP.NET C# application using Entity Framework to access SQL Server? I have my database, but I need to fill a combobox with data from another database.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    by declaring two different context objects, and using the second one to query the other database? What does your code look like now? What have you tried? What went wrong? – ADyson Jul 08 '19 at 15:11
  • You could run into an issue if you are trying to save data from related tables (they need to be in same context). Another option might be setting up a database view to the other table, but you need to provide a lot more info on what you are aiming for. – Steve Greene Jul 08 '19 at 15:13
  • ADyson: it does not allow me to use the two models in a controller, it does not recognize the second model, I want to know how to fill a combobox with data from the second model – Sol Ovalles Jul 08 '19 at 15:27
  • Steve Greene: my goal is to fill combobox with data of the second model, to show in the view, the second model is only used to consult data and fill in grid and combobox – Sol Ovalles Jul 08 '19 at 15:29
  • 1
    That's a slightly separate issue. You can use a ViewModel objects which contains data from both databases. Or you could put the list data into the ViewBag. How you get the data into your view is separate from the issue of how get the data from the database. It's not generally considered good practice to tightly couple your view to your database by using the data models as the types for your views.ost people suggest using ViewModels in all cases as an intermediary – ADyson Jul 08 '19 at 15:59
  • Yeah, that has nothing to do with contexts then. See [here](https://stackoverflow.com/questions/5550627/two-models-in-one-view-in-asp-mvc-3) – Steve Greene Jul 08 '19 at 19:36

0 Answers0