5

I'm querying for ProductRisk, which contains a Status property, where Status is an enum. Here's the mapping for ProductRisk:

public class ProductRiskMap : ClassMap<ProductRisk>
{
    public ProductRiskMap()
    {
        Table("AccountManagement.dbo.ProductRisk");

        Id(x => x.Id, "ProductRiskID");

        References(x => x.AssociatedProduct, "ProductID");
        References(x => x.AssociatedClient, "EntityId");

        Map(x => x.Reason, "ProductRiskReasonID").CustomType<int>();
        Map(x => x.Status, "RiskStatusID").CustomType<int>();
    }

Status is an enum with four possible values. It's represented in the database as a foreign key reference to a lookup table. In my repository, I want to pull the ProductRisk objects with a Status of Medium or High. The following query in Ling To NHibernate works:

    public IList<ProductRisk> GetByClient(int[] clientIds)
    {
        return NHibernateSession.Current.Query<ProductRisk>()
            .Where(x => clientIds.Contains(x.AssociatedClient.Id))
            .Where(x => x.Status == RiskStatus.Medium || x.Status == RiskStatus.High)                
            .ToList();
    }

But if I use (what I think is) the equivalent query in the Criteria API:

        return NHibernateSession.Current.QueryOver<ProductRisk>()
            .WhereRestrictionOn(x => x.AssociatedClient.Id).IsIn(clientIds)
            .Where(x => x.Status == RiskStatus.Medium || x.Status == RiskStatus.High)
            .List();

I receive the following error:

Type mismatch in NHibernate.Criterion.SimpleExpression: Status expected type System.Int32, actual type FIS.AccountManagement.Core.Domain.RiskStatus

Why is that? Thanks in advance for any help.

Vish
  • 453
  • 6
  • 18

1 Answers1

4

when i want to map enums to ints in FluentNH i specify the enum as custom type then this should do:

Map(x => x.Status, "RiskStatusID").CustomType<RiskStatus>();
Firo
  • 30,626
  • 4
  • 55
  • 94