Strange linq to nhibernate issue, Invalid cast from 'System.Int32?

I believe the problem is that Linq/Nbernate is trying to map IEntity. Id to a table column instead of TEntity.Id. I had this problem with a LinqToSql repository implementation.

The way around it was to use an expression like this: private static Expression> GetFindExpression(string propertyName, object value) { ParameterExpression parameterExpression = Expression. Parameter(typeof (TEntity), "id"); MemberExpression propertyExpression = Expression. Property(parameterExpression, propertyName); Expression bodyExpression = Expression.

Equal(propertyExpression, Expression. Constant(value)); return Expression. Lambda>(bodyExpression, parameterExpression); } So this would change Get(id) to: public TEntity Get(int id) { var entities = Query.

Where(GetFindExpression("Id", id)); return entities.FirstOrDefault(); } Update: If you don't want to deal with expressions (they can be tricky! ), you could use the Dynamic LINQ library as described by Scott Guthrie here: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx That would change Get(id) to: public TEntity Get(int id) { var entities = Query. Where("Id = @0", id); return entities.FirstOrDefault(); }.

I believe the problem is that Linq/Nbernate is trying to map IEntity. Id to a table column instead of TEntity.Id. " I totally agree.

I like the idea of transforming the lambda, I'm going to try and make a generic version which will handle all transforms... stay tuned – Andrew Bullock Mar 20 '09 at 10:19.

Linq to Nbernate is currently being rewritten, im working around this until its released.

I've ran into this in the past and it usually boils down to the field in the database table you're reading is not in a compatible format. Like booleans don't convert to integers. Check your field types in the table and make sure they are compatible.

Maybe your Id column is a BIGINT?

I agree that could cause an "invalid cast" exception, but thats not whats going on here. Read the code and the bug report on jira. Its a bug caused by the generics – Andrew Bullock Mar 19 '09 at 11:22.

I am having the same problem :( The following does not work. Public override T Load(int id) { return (from t in _sessionFactory.Session.Linq() where t. ID == id select t).SingleOrDefault(); } The following does!

Public override Product Load(int id) { return (from t in _sessionFactory.Session.Linq() where t. ID == id select t).SingleOrDefault(); }.

The bug has been reported but not yet fixed: nhjira.koah.net/browse/NHLQ-11 I have posted a simple test case to reproduce it. Let's hope it get's addressed soon!

I noticed you added a test case, nice work. Hopefully they'll fix this soon! – Andrew Bullock Aug 10 '09 at 13:23.

Invalid cast from 'System. Where(x => (Convert(x). Id = value(CRUDService`1+c__DisplayClass0Contract).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions