2

I am testing lazy loading in a C# Console Application. For some reason Lazy loading is not working.

I have checked the LazyLoadingEnabled and ProxyCreationEnabled properties of the context.Configuration. They are true.

My property is virtual.

I have checked the other similar SO questions without success. I am not sure what might be happening.

This is my code (simplified to not show namespaces):

static void Main(string[] args) {

    Models.DataContext dc = new Models.DataContext();

    Console.WriteLine("Context Lazy {0}. Proxy Creation {1} ", 
                       dc.Configuration.LazyLoadingEnabled,
                       dc.Configuration.ProxyCreationEnabled);

    var grp = dc.Groups.FirstOrDefault();     
    Console.WriteLine("GroupId {1}, AttrSet is null = {0}", 
                       grp.AttrSet == null , grp.Id); 

    var grp2 = dc.Groups.Include("AttrSet").FirstOrDefault();
    Console.WriteLine("GroupId {1}, AttrSet is null = {0}", 
                              grp2.AttrSet == null, grp2.Id); 
}

class Group  {
    public System.Guid Id { get; set; }
    public string Name { get; set; }
    public virtual AttrSet AttrSet { get; set; }
 }

class AttrSet {
    public System.Guid Id { get; set; }
    public string Name { get; set; }
}

The output of running this is:

Context Lazy True. Proxy Creation True

GroupId 186ebc8a-dec7-4302-9f84-5a575577baac, AttrSet is null = True

GroupId 186ebc8a-dec7-4302-9f84-5a575577baac, AttrSet is null = False

I am sure that the loaded record is correct and it has a proper AttrSet in the database.

Any ideas?

Update

I created a very simple testing project in case any one actually wants to look at the code.

See: https://bitbucket.org/josea/eflazy (GIT: https://[email protected]/josea/eflazy.git).

2
  • I tried reproducing to no avail. What version of EF are you running? Also, instead of testing against NULL when querying w/o the Include() call, try accessing a property and see if you get a null ref exception Commented Mar 25, 2013 at 22:50
  • @Moho: I tried that, it fails. I have posted source code. Thanks. Commented Mar 27, 2013 at 12:35

3 Answers 3

4

Proxy generation is not occurring. Why?? Because your POCOs are PRIVATE!! EF can't see them to derive proxies from them. Make your POCOs public and it'll work the way you expect.

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

1 Comment

That was it. I wonder if there is documentation somewhere about it. Thank you :)
0

Are you using anything to configure the 1:1 relationship between the 2 classes? Because it doesn't look like you are here, which would cause Entity Framework to not be able to load the relationship.

You can use Data Annotations to define the FK relationship as so:

public class AttrSet {

  public System.Guid Id { get; set; }
  public string Name { get; set; }

  [Required, ForeignKey("MyGroup")]
  public int GroupID { get; set; }

  public virtual Group MyGroup { get; set; } 
}

6 Comments

this is not a 1:1 relationship. It is a 1:N (1 AttrSet could be used by many Groups). I added the ICollection<Group> InGroups properties to AttrSet, but still doesn't work. BTW: I believe that the relationship is configured by the AttrSet property in Group.
Is that the reason for the -1?
IronMan84: yes. Do not take it badly, but your response doesn't correspond to the question. The point system is to promote the answers to the questions. Maybe I am being too precise. I +1 your comment. BTW: I appreciate your help even if I haven't still solved my issue. Thanks.
I understand that that's the system. I've been doing this site for a while (3000+ points, after all). I also recognize, though, that a -1 for an answer to a question that was phrased badly is quite unfair. How exactly was anyone supposed to divine the relationship that you intended between the 2 tables? From the way you phrased the question it wasn't loading the child entity of the Group table, which is more often than not a relationship problem. I understand that it doesn't answer your question as you intended. But -1 for that? Not fair.
Well... I returned your point by voting up your complain (right?). I didn't mean it to be some sort of punishment or anything (just avoiding getting the answer to be in the top when it doesn't answer the question). When the system allows me I will up-vote the answer as well (it doesn't let me right now because the answer hasn't changed). I honestly don't care about the points that much (however, somebody downvote the question itself, maybe you, and that hurts because the question gets less exposure).
|
0

This should give you the relationship you wanted. AttrSetId is whatever you've named the FK column in your table, so change that if it is different.

public class Group  {

         public System.Guid Id { get; set; }
         public string Name { get; set; }
         public System.Guid AttrSetId {get;set;}         

         [ForeignKey("AttrSetId")]
         public virtual AttrSet AttrSet { get; set; }
 }

Edit:

Add this line to your AttrSet class:

public virtual ICollection<Group> Groups {get;set;}

Add this next line to your OnModelCreating in your Models.DataContext. If for some reason you don't already have that function overridden, it'll look like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<Group>().HasOptional(x => x.AttrSet).WithMany(x => x.Groups);
}

I put HasOptional instead of HasRequired as I assumed you could save a Group without an AttrSet. If that is not true, and the FK is not nullable, then you should use HasRequired.

5 Comments

Sorry - that only allows me to customize the name of the foreign key field. I tested it (before) and it doesn't work. If I do not use this the system just uses AttrSet_Id as the field (which is the standard). Please note that EF understands the relationship enough so if I use the "include" method it works (ie: eager loading).
Malcolm: tested this. It didn't work :(. I have updated the question's code to show that the "Include" works. Note: I added the code my AttrSet, Group and DataContext as suggested by you, but I believe it is redudant (I think EF knows the 1:N relationship by just the AttrSet property in Groups). I am keeping that code anyways (while I test). Thanks for the help.
I tried reproducing to no avail. What version of EF are you running? Also, instead of testing against NULL when querying w/o the Include() call, try accessing a property and see if you get a null ref exception.
EF Version 5.0 + .NET 4.5. Tried accessing the property and it crashes with null ref exception. Thanks. BTW: I am not sure if you are saying that it works for you.
On my machine your original code worked (no attributes, no FluentApi, had to make the classes public). I am perplexed that it is not working for you. I am running VS Express 2012, .Net 4.5 with the latest EF from NuGet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.