5

I have defined a (poco?) class in my domain project:

public class Club
{
   public Club()
   {
      ContactPersons = new HashSet<ContactPerson>();
   }

   public int Id { get; set; }

   [Required]
   [StringLength(64)]
   public string Name { get; set; }

   public virtual ICollection<ContactPerson> ContactPersons { get; set; }
}

public class ContactPerson
{
     public virtual int Id { get; set; }

     [StringLength(64)]
     public virtual string FirstName { get; set; }

     [StringLength(64)]
     public virtual string LastName { get; set; }
 }

In my MVC project I have my clubcontroller:

   public ActionResult Create(CreateClubViewModel model)
   {
      Club club = new Club();
      model.Initialize(club);
      IClubDb clubDb = DependencyResolverHelper.IClubDbService;
      clubDb.Create(club);  // create club in db
   }

    public ActionResult Display(string domain)
    {
        try
        {
            IClubDb clubDb = DependencyResolverHelper.IClubDbService;
            Club club = clubDb.Get(domain);
            return View(club);
        }
        catch (Exception)  // user is not logged iin
        {
            return View();
        }
    }

Finally, in my DB project I create and retrieve the club,

public Club Get(string name)
{
   return DataContext.Clubs
   //.Include(x => x.ContactPersons)
   .Single(r => r.Name == name);
}

 public int Create(Club club)
 {
         DataContext.Clubs.Add(club);
         return DataContext.SaveChanges();
 }

I have tried everything to get EF to lazy load the ContactPersons of my club object when I call the Get club in the Display method but ContactPersons has always a length of zero. However, if I eager load contact persons using the .include (I have commented this part out), then obviously ContactPersons contains a number of contacts.

I am not sure what I am doing wrong:

  1. I have followed the guidelines for defining poco classes: http://msdn.microsoft.com/en-us/library/dd468057.aspx
  2. I have a public parameter less constructor (but not protected constructor)
  3. I have lazyloading enabled

I think I am missing a concept, the poco club class is also my domain entity which I insert into DB. What am I doing wrong? Whay I can't get lazy loading to work?

7
  • Try using a List<ContactPerson> instead of an ICollection<ContactPerson>. I don't think EF is clever enough to use ICollection's Commented Jan 1, 2013 at 21:38
  • Dominic, just tried your suggestion, still the same, ContactPersons is empty :( Commented Jan 1, 2013 at 22:14
  • @Dominic: ICollection<T> works quite fine. It even makes more sense since a List implies an order, and related entities are not returned in any particular order. Commented Jan 1, 2013 at 22:16
  • Did you enabled the proxy creation ? Commented Jan 2, 2013 at 5:29
  • Yes Jayantha, I have these two lines when I intialize the db context: this.Configuration.LazyLoadingEnabled = true; this.Configuration.ProxyCreationEnabled = true; Commented Jan 2, 2013 at 20:09

3 Answers 3

1

try ContactPersons.ToList();

this will force all entities to be loaded. see Entity framework, when call ToList() it will load FK objects automatically?

Sign up to request clarification or add additional context in comments.

Comments

0

It seems that your LazyLoading performs when your dbContext is closed. So it will not load. You use ContactPerson in view, am i right?

2 Comments

Yes, I access the club in View(club) of Display method. But I did try to access the contact person before the line "return view (club") and it was the same. I also tried to access the contact person when I get the club within DB project and it was not loaded.
When I use the debugger, the type of ContactPerson is not changed. I remember vaguely that EF creates a new type and inserts a hook in the getter accessor function. It appears that EF is not creating proxy objects at all, hence lazy loading fails. I have the following lines in my code: this.Configuration.LazyLoadingEnabled = true; this.Configuration.ProxyCreationEnabled = true;
0

Did you forget to include the foreign key in your entity?

public class ContactPerson
{
    public virtual int Id { get; set; }

    [StringLength(64)]
    public virtual string FirstName { get; set; }

    [StringLength(64)]
    public virtual string LastName { get; set; }

    public int ClubId { get; set; }
    [ForeignKey("ClubId")]
    public virtual Club Club { get; set; } // if you need
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.