Fluent NHibernate table-per-class-hierarchy need to use .Where()?

江枫思渺然 提交于 2019-12-24 03:53:28

问题


I have the following mapping for a set of contact classes based off an abstract Contact class implementation.

   public class ContactMapping : ClassMap<Contact> {
        public ContactMapping() {
            Id(x => x.Id).GeneratedBy.GuidComb();
            Map(x => x.CreatedDate).Not.Nullable();
            Map(x => x.Value).Not.Nullable();
            Map(x => x.Level).Not.Nullable();
            Map(x => x.Comments);
            DiscriminateSubClassesOnColumn("ContactType");
        }
    }

    public class PhoneContactMapping : SubclassMap<PhoneContact> {
        public PhoneContactMapping() {
            Map(p => p.PhoneType);
            DiscriminatorValue("PhoneContact");
        }
    }
    public class EmailContactMapping : SubclassMap<EmailContact> {
        public EmailContactMapping() {
            DiscriminatorValue("EmailContact");
        }

    }
    public class WebsiteContactMapping : SubclassMap<WebsiteContact> {
        public WebsiteContactMapping() {
            DiscriminatorValue("WebsiteContact");
        }
    }

I have an entity class that HasMany EmailContact(s), WebsiteContact(s), and PhoneContact(s).

public class ContactableEntityMapping: ClassMap<ContactableEntity> {
    public ContactableEntityMapping() {
        Id(x => x.Id).GeneratedBy.GuidComb();
        Map(x => x.CreatedDate).Not.Nullable();
        Map(x => x.Comment);
        HasMany<EmailContact>(x => x.EmailContacts).AsBag().Not.LazyLoad().Where("ContactType='EmailContact'");
        HasMany<PhoneContact>(x => x.PhoneContacts).AsBag().Not.LazyLoad().Where("ContactType='PhoneContact'");
        HasMany<WebsiteContact>(x => x.WebsiteContacts).Not.LazyLoad().AsBag().Where("ContactType='WebsiteContact'");
        HasManyToMany(x => x.Addresses).AsSet();
    }
}

If i do not specify there .Where() clauses the class ends up coming back with all rows mapped to EmailContact (since it is first in the listing) if LazyLoading is used, and if lazy-loading is not used I receive an exception as it attempts to cast the classes to the wrong type.

Obviously this is because the SQL executed is not passing in the additional where clause unless I specify it in the mapping. Am I missing something in my mapping, or is the Where mess just something we need to live with?

Thanks for the help!

来源:https://stackoverflow.com/questions/1641124/fluent-nhibernate-table-per-class-hierarchy-need-to-use-where

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