I am working in EF 4.3.1 and everything is going pretty well on the Insert side. But now I am trying to pull the objects I inserted back out to show in a View in my MVC3 project and having problems.
In my DAL, I have the following method to get a Church
object from the database:
public virtual TEntity GetById(object id)
{
return _db.Find(id);
}
The object itself looks like this:
public class Church
{
//Basic Properties
public int Id { get; set; }
public string Name { get; set; }
public string Website { get; set; }
... (some properties)
//Church Contacts and Services
public List<Contact> Contacts { get; set; }
public List<Service> Services { get; set; }
....
}
Notice the last two Properties: Contacts and Services. Each is a 1:Many table within my database. It appears that Find()
does not return any of this, though. It just sets the two properties to null.
The actual entries in the database look just how I would expect them to. Each Church has a couple contacts and services associated with them. Yet, it never maps back to the model...
Any thoughts?
EDIT Yeah, looks like it wasn't loading those properties until I explicitly told it to. I modified my query by adding this to my ChurchRepository (inherits from GenericRepository)
public override Church GetById(object id)
{
return _db.Include("Contacts").Include("Services").FirstOrDefault(c => c.Id == (int)id);
}