Search code examples
c#.netnhibernatefluent-nhibernatenhibernate-mapping

Mapping by code Zero-Or-One relationship


I have the following database structure, which I cannot change:

CREATE TABLE Users (
    ID INT NOT NULL IDENTITY(1,1),
    PRIMARY KEY(ID)
)

CREATE TABLE UserAvatars (
    UserID INT NOT NULL,
    Width INT NOT NULL,
    Height INT NOT NULL,

    PRIMARY KEY(UserID),
    FOREIGN KEY(UserID) REFERENCES Users(ID),
)

Basically, a User can have Zero-or-one avatars (I removed columns to simplify the example).

class User
{
    public int ID { get; protected set; }

    public UserAvatar Avatar { get; set; }
}

class UserAvatar
{
    public User User { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
}

I am using the following mapping:

class UserMapping : ClassMapping<User>
{
    public UserMapping() 
    {
        Id(x => x.ID, m => {
            m.Generator(Generators.Identity);
        });

        OneToOne(x => x.Avatar);
    }
}

class UserAvatarMapping : ClassMapping<UserAvatar>
{
    public UserAvatar() 
    {
        Id(x => x.User, m =>
        {
            m.Generator(Generators.Foreign<User>(u => u.ID));
        });

        Property(x => x.Width);
        Property(x => x.Height);
    }
}

However, when trying run my app, I get an NHibernate.MappingException:

NHibernate.MappingException : Could not compile the mapping document: mapping_by_code----> NHibernate.MappingException : Could not determine type for: MyApp.Models.UserAvatar, MyApp, for columns: NHibernate.Mapping.Column(User)

I can't make any sense of this cryptic error.

How do I accomplish the mapping where a User could have zero or one Avatars?


Solution

  • The ID is important almost for every entity mapped by NHibernate (ORM). So, we should extend the Avatar class:

    class UserAvatar
    {
        // every(stuff) should have its ID
        public int ID { get; protected set; } 
        public User User { get; set; }
        ...
    

    The ID value will be managed by one-to-one relationship. So, now let's adjust the mapping.

    public UserAvatar() 
    {
        Id(x => x.ID);
    
        HasOne(x => x.User)
          .Constrained()
          .ForeignKey();
        ...
    
    public UserMapping() 
    {
        Id(x => x.ID);
        HasOne(s => s.Avatar).Cascade.All();
    

    Related and really interesting reading: