Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to NHibernate query comparing enum mapped as integer succeeds, but fails as equivalent criteria query

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.

like image 258
Vish Avatar asked Oct 09 '22 12:10

Vish


1 Answers

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>();
like image 142
Firo Avatar answered Oct 12 '22 23:10

Firo