views:

109

answers:

0

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!