A Linq to NHibernate query comparing an enumeration converted as a whole succeeds but is not executed as a query for equivalent criteria

I am requesting a ProductRisk that contains the Status property, where Status is an enumeration. Here is 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 enumeration with four possible values. It is presented in the database as an external key link to the lookup table. In my repository, I want to pull out ProductRisk objects with Medium or High status. 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) an 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 get the following error:

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

Why? Thank you in advance for any help.

+4
source share
1 answer

when I want to map enums to ints in FluentNH, I specify enum as a user type, then this should do:

 Map(x => x.Status, "RiskStatusID").CustomType<RiskStatus>(); 
+4
source

Source: https://habr.com/ru/post/1380832/


All Articles