简体   繁体   中英

Fluent nHibernate - not Lazy Loading

It's probably something silly but I can get my head around it. I have three tables

Post
id
content
...
User 
id
name
...
Vote
user_id
post_id
votedate

My VoteMap:

public class VoteMap : ClassMap<Vote>
{
    public VoteMap()
    {
        DynamicUpdate();

        Table("vote");            
        Id(Reveal.Member<Vote>("_id")).Column("id");
        Map(Reveal.Member<Vote>("_votedate")).Column("vote_date");

        References(x => x.Post).Column("post_id").LazyLoad().Not.Nullable();
        References(x => x.User).Column("user_id").LazyLoad().Not.Nullable();
    }
}

PostMap:

public class PostMap : ClassMap<Post>
{
    public PostMap()
    {
        DynamicUpdate();

        Table("post");
        Id(Reveal.Member<Post>("_id")).Column("id");
        Map(Reveal.Member<Post>("_content")).Column("content");
        References(x => x.User).Column("user_id").Not.Nullable().LazyLoad();
    }
}

UserMap

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        DynamicUpdate();

        Table("user");
        Id(Reveal.Member<User>("_id")).Column("id");
        Map(Reveal.Member<User>("_name")).Column("name");

        HasMany(x => x.Posts).KeyColumn("user_id").LazyLoad();
        HasMany(x => x.Votes).KeyColumn("user_id").LazyLoad();
    }
}

When I retrieve Vote:

var myvote = _votesRepository.GetVote(postid, userid);
int post_id = myvote.Post.Id;
int user_id = myvote.User.Id;

Vote attributes are set correctly. Post and User references are of type Castle.Proxies.PostProxy and Castle.Proxies.UserProxy and their attributes are empty. ( post_id and user_id are set to 0)

Shouldn't nHibernate lazy load this data?

You were right jbl, it doesn't like private id fields at all. This change solved the problem:

protected int _id;
public virtual int Id { get {return _id;} }

and changing my mapping for all classes from this:

Id(Reveal.Member<Vote>("_id")).Column("id");

to this:

Id(x => x.Id).Column("id");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM