NHibernate LINQ System.TypeLoadException exception

246 views
Skip to first unread message

kamil.kliczbor

unread,
Dec 28, 2010, 3:48:35 PM12/28/10
to nhusers
Hi,
I wanted to try LINQ feature with NHibernate 3.0.0.4000. When I write
a query in ICriteria API it works. But with LINQ I get:
System.TypeLoadException : Method 'CreateAlias' w typie
'NHibernate.Linq.Util.DetachedCriteriaAdapter' from assembly
'NHibernate.Linq, Version=1.0.0.4000, Culture=neutral,
PublicKeyToken=444cf6a87fdab271' has no implementation.

Mappings are fine, because all my CRUD tests are ok.

Here is the query that works:
this.Session.CreateCriteria(typeof(Product))
.Add(Expression.Between("ReorderLevel", 1, 3))
.List<Product>();

and the LINQ that doesn't:
this.Session.Linq<Product>()
.Where(p => p.ReorderLevel >= 1 && p.ReorderLevel <=
3)
.ToList();

StackTrace:
w NHibernate.Linq.Util.CriteriaUtil.GetRootType(ICriteria criteria)
w
NHibernate.Linq.Visitors.MemberNameVisitor.IsRootEntity(EntityExpression
expr)
w
NHibernate.Linq.Visitors.MemberNameVisitor.VisitEntity(EntityExpression
expr)
w
NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression
exp)
w
NHibernate.Linq.Visitors.NHibernateExpressionVisitor.VisitPropertyAccess(PropertyAccessExpression
expr)
w
NHibernate.Linq.Visitors.MemberNameVisitor.VisitPropertyAccess(PropertyAccessExpression
expr)
w
NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression
exp)
w NHibernate.Linq.Visitors.MemberNameVisitor.GetMemberName(ICriteria
rootCriteria, Expression expr)
w
NHibernate.Linq.Visitors.BinaryCriterionVisitor.VisitPropertyAccess(PropertyAccessExpression
expr)
w
NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression
exp)
w
NHibernate.Linq.Visitors.BinaryCriterionVisitor.GetBinaryCriteria(ICriteria
rootCriteria, ISession session, BinaryExpression expr,
ComparePropToValue comparePropToValue, ComparePropToProp
comparePropToProp, CompareValueToCriteria compareValueToCriteria,
ComparePropToCriteria comparePropToCriteria)
w
NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitBinaryCriterionExpression(BinaryExpression
expr)
w
NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitBinary(BinaryExpression
expr)
w NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
w
NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression
exp)
w
NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitAndAlsoExpression(BinaryExpression
expr)
w
NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitBinary(BinaryExpression
expr)
w NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
w
NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression
exp)
w
NHibernate.Linq.Visitors.ExpressionVisitor.VisitLambda(LambdaExpression
lambda)
w NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
w
NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression
exp)
w
NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitUnary(UnaryExpression
expr)
w NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
w
NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression
exp)
w
NHibernate.Linq.Visitors.WhereArgumentsVisitor.GetCriterion(ICriteria
rootCriteria, ISession session, Expression expression)
w
NHibernate.Linq.Visitors.RootVisitor.HandleWhereCall(MethodCallExpression
call)
w
NHibernate.Linq.Visitors.RootVisitor.VisitMethodCall(MethodCallExpression
expr)
w NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
w
NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression
exp)
w
NHibernate.Linq.Visitors.NHibernateQueryTranslator.Translate(Expression
expression, QueryOptions queryOptions)
w
NHibernate.Linq.NHibernateQueryProvider.TranslateExpression(Expression
expression)
w NHibernate.Linq.NHibernateQueryProvider.Execute(Expression
expression)
w NHibernate.Linq.Query`1.GetEnumerator()
w System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
w System.Linq.Enumerable.ToList(IEnumerable`1 source)
w Northwind.Domain.Tests._101Queries.Restrictions.BetweenClause() w
Restrictions.cs: line 75

Here is my Fluent mapping:
public class ProductClassMap : ClassMap<Product>
{
public ProductClassMap()
{
Table("Products");
Id(x => x.Id, "ProductId");
Map(x => x.Name, "ProductName").Length(40).Not.Nullable();
References(x => x.Supplier,
"SupplierId").Fetch.Select().Access.Property();
References(x => x.Category,
"CategoryId").Fetch.Select().Access.Property();
Map(x => x.QuantityPerUnit,
"QuantityPerUnit").Length(20).Access.Property();
Map(x => x.UnitPrice, "UnitPrice").Access.Property();
Map(x => x.UnitsInStock,
"UnitsInStock").Access.Property();
Map(x => x.UnitsOnOrder,
"UnitsOnOrder").Access.Property();
Map(x => x.ReorderLevel,
"ReorderLevel").Access.Property();
Map(x => x.Discontinued,
"Discontinued").Not.Nullable().Access.Property();
HasMany(x => x.OrderLines)
.KeyColumn("ProductId")
.Cascade.None()
.AsBag()
.Access
.CamelCaseField(Prefix.Underscore);
}
}

Here is my entity:
public class Product : Entity<Product>
{
private readonly IList<OrderLine> _orderLines;
private string _name;

public Product() : this(null)
{
}

public Product(string name)
{
_orderLines = new List<OrderLine>();

_name = name;
}

public virtual string Name
{
get { return _name; }
set { _name = value; }
}

public virtual Supplier Supplier { get; set; }

public virtual ProductCategory Category { get; set; }

public virtual string QuantityPerUnit { get; set; }

public virtual decimal? UnitPrice { get; set; }

public virtual int UnitsInStock { get; set; }

public virtual int UnitsOnOrder { get; set; }

public virtual int ReorderLevel { get; set; }

public virtual bool Discontinued { get; set; }

public virtual ReadOnlyCollection<OrderLine> OrderLines
{
get { return new ReadOnlyCollection<OrderLine>(_orderLines); }
}
}


Diego Mijelshon

unread,
Dec 28, 2010, 8:53:56 PM12/28/10
to nhu...@googlegroups.com
You are using the wrong provider. NH 3 has an integrated one, accessible by using the Query<T> extension method; you can remove the old NHibernate.Linq assembly, which is not compatible.
 
    Diego




--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


kamil.kliczbor

unread,
Dec 29, 2010, 2:54:50 PM12/29/10
to nhusers
Thanks, it really helped me alot :)

By the way. There is quite interesting issue with writing simple
queries. When I attach "true" to the property in NHLQ Where clause I
get strange SQL generated:

When there is
this.Session.Query<Product>()
.Where(x => x.Discontinued)
.ToList();

it produces select statement given below:

select ...
from Products product0_
where product0_.Discontinued = 1

, but when I use:
this.Session.Query<Product>()
.Where(x => x.Discontinued == true)
.ToList();

is gives:

select ...
from Products product0_
where case
when product0_.Discontinued = 1 then 'true'
else 'false'
end = case
when 'True' /* @p0 */ = 'true' then 'true'
else 'false'
end

Isn't that quite strange behaviour ?

Diego Mijelshon

unread,
Dec 29, 2010, 6:27:37 PM12/29/10
to nhu...@googlegroups.com
Yes it is... you can create a ticket in http://jira.nhforge.org
 
    Diego



--
Reply all
Reply to author
Forward
0 new messages