NHibernate explicit fluent column mapping

我们两清 提交于 2020-01-23 13:18:25

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/1903988/nhibernate-explicit-fluent-column-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!