Being new to MVC and the Entity Framework I've had quite a few problems performing complex data queries. I'm using a Database First approach I need to understand the best way to pull related data from a separate database. I have connected to the databases and created the edmx files as well as the models from the existing databases. I will simplify the problem example: If I have one table with employee information:
Employee_Information(EmployeeID, FullName, Address, Phone)
Then I have a second (in another database)
Employee_Achievements(EmployeeID, Achievement, DateAchieved)
How can I tie these two together to pull the employee's information (name, address, etc.) in a view? My initial thought was do a join and then pass that to the view:
var employee = from emp in db.EmployeeAchievements
join empInfo in emp_db.EmployeeInformation
emp.EmployeeID == empInfo.EmployeeID
The problem I have with this is that I'm already using a ViewModel with multiple models being passed to the view. I need the ability to tie the information in for several employees at a time with something like this in the view:
@for (int i = 0; i < model.EmployeeAchievements.Count; i++)
{
<tr valign="top">
<td>
<div class="editor-field">
@Html.EditorFor(model => model.EmployeeAchievements[i].EmployeeID)
@Html.ValidationMessageFor(model => model.EmployeeAchievements[i].EmployeeID)
</div>
</td>
// Display employee name next
Possible use of the ViewBag? Or is there a way that I can associate the foreign key across databases? Keep in mind this is Database First so the edmx and model files are generated. I haven't been able to find any good examples of what I'm trying to do here. I would appreciate any help and can provide more detail if needed.