0

Can anyone help as to why my onetoone properties won't load? I have another project where they work fine, but for some reason in this one neither of my two properties will work.

Mapping:

public class PlayerMap : ClassMap<Player>
{
    public PlayerMap()
    {
        Table("Player");
        LazyLoad();

        Id(x => x.PlayerId).GeneratedBy.Identity().Column("PlayerId");

        HasOne(x => x.Stats).ForeignKey("PlayerId");
        HasOne(x => x.Rankings).ForeignKey("PlayerId");

        Map(x => x.LastName).Column("LastName");
        Map(x => x.FirstName).Column("FirstName");

        HasMany(x => x.MatchResults).KeyColumn("PlayerId");
    }
}

Properties:

public virtual Stats Stats { get; set; }
public virtual Rankings Rankings { get; set; }

In the database, they are setup with a Foreign Key relationship.

Where am I going wrong?

ganders
  • 7,285
  • 17
  • 66
  • 114
  • Why do you explicitly specify `Table("Player")`? This is default behavior. Same goes for `Column({name})`, it's superfluous. For the `ForeignKey({name})` you should use [`Static Reflection`](http://www.jagregory.com/writings/introduction-to-static-reflection/) thus it becomes like `ForeignKey(GetProperty(p => p.Id); )`. – M. Mimpen Feb 21 '14 at 09:54
  • Just the way I've always done. When I first started with Fluent NHibernate, I was mostly copying code snippets and didn't know the specifics of how/what I could do. Now it's just by habit... – ganders Feb 21 '14 at 13:18
  • My suggestion would be to delete such code since it is hard-coded unneeded code, violating the DRY principle. But please make your own informed choice :D – M. Mimpen Feb 21 '14 at 13:23

1 Answers1

1

This answer seems to be what you're looking for. Assuming Player is considered the parent and Stats/Rankings are the children, your mapping should look something like this:

//PlayerMap
HasOne(x => x.Stats).PropertyRef(r => r.Player).Cascade.All();
HasOne(x => x.Rankings).PropertyRef(r => r.Player).Cascade.All();

//StatsMap
References(x => x.Player, "PlayerId").Not.Nullable();

//RankingsMap
References(x => x.Player, "PlayerId").Not.Nullable();
Community
  • 1
  • 1
JW Lim
  • 1,794
  • 3
  • 21
  • 41
  • Can I still do the mapping if a ranking, or statistics are not required? Or does that break the structure? I'm assuming if that's possible then I can't do the .Cascade.All()..., right? (Obviously I'm going to test it out, thanks) – ganders Feb 18 '14 at 00:33
  • @ganders I don't see why that would break the structure. It ought to be fine, since from what I know the Cascade only takes effect if there is actually an association (or I could be wrong). Try testing it out and let me know! :) – JW Lim Feb 18 '14 at 00:36