I have developed a fairly small asp.net MVC application using the repository pattern and Linq to Sql. I would now like to access and update the same data using the Entity Framework, however I am having trouble understanding if my syntax is correct for table relationships in the Entity Framework.
Say I am retrieving a row of data for a User and I would also like to retrieve the User's assosiated Business (foreign key in the User table). Right now I am doing something like this:
using (MyDatabaseEntities context = new MyDatabaseEntities())
{
User user = db.User.FirstOrDefault(u => u.Users.UserId == userId);
if (!user.BusinessReference.IsLoaded)
{
user.BusinessReference.Load();
}
return user;
}
From the examples I've seen I should do something like this to access the business table data:
foreach (Business business in user.BusinessReference)
{
var b = business;
ViewData["BusinessName"] = b.BusinessName;
}
Am I going about this the right way? Any advice would be greatly appreciated.