Fluent NHibernat - Querying enum field with LINQ
My application has the following database structure:
Transactions:
- TransactionID (PK, Identity, Int)
- TypeID (FK, Int)
- Amount (Decimal)
TransactionTypes:
- TypeID (PK, Identity, Int)
- Type (NVarChar)
They are defined in my application as:
public class Transaction
{
public virtual int TransactionID { get; set; }
public virtual TransactionTypes Type { get; set; }
public virtual decimal Amount { get; set; }
}
public enum TransactionTypes
{
Event = 1,
Product = 2
}
With the following mapping:
public class TransactionMap : ClassMap<Transaction>
{
public TransactionMap()
{
Table("Transactions");
Id(x => x.TransactionID);
Map(x => x.Type, "TypeID").CustomType<int>();
Map(x => x.Amount);
}
}
Everything works fine a开发者_Python百科part from querying. When i try doing:
session.Linq<Transaction>().Where(t => t.Type == TransactionTypes.Event).ToList();
It throws the error "Type mismatch in NHibernate.Criterion.SimpleExpression
: Type expected type System.Int32
, actual type Entities.TransactionTypes
".
I'd appreciate it if someone could show me the correct way to map this. Thanks
The best way to map enums in Fluent NHibernate is by using conventions. See these two threads for help:
Mapping enum with fluent nhibernate
How do you map an enum as an int value with fluent NHibernate?
精彩评论