NHibernate explicit fluent column mapping

2019-05-28 11:41发布

I have a set of fluent object mappings that looks like this:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Map(x => x.Id);
        Map(x => x.Status);
    }
}

public class SpecialUserMap : SubClassMap<SpecialUser>
{
    public SpecialUserMap()
    {
        Map(x => x.Property);
    }
}

public class DirectoryMap : ClassMap<Directory>
{
    public DirectoryMap
    {
        Map(x => x.Id);
        HasMany(x => x.SpecialUsers).Where("Status = 0");
    }
}

User is a join table, which SpecialUser joins against to get things like status. However, when I try to reference a SpecialUser in Directory's SpecialUsers collection, I get an error of "Undefined column 'Status'", as in the generated SQL, NHibernate tries to grab the Status column from the SpecialUser table, and not the User table. Is there a way to explicitly tell NHibernate which table to get the Status column in the DirectoryMapping?

2条回答
forever°为你锁心
2楼-- · 2019-05-28 12:26

The Status property of a User / SpecialUser needs to map onto a single column in the database. You can't have it coming sometimes from User and sometimes from SpecialUser.

As a workaround, you could add a SpecialUserStatus property to SpecialUser, and then you could query on that easily.

查看更多
Rolldiameter
3楼-- · 2019-05-28 12:39

That mappings looks right for table-per-subclass mapping, assuming that SpecialUser extends User. My guess is that it's a bug.

查看更多
登录 后发表回答