locked
System.InvalidCastException: Specified cast is not valid. Fluent NHibernate and C# RRS feed

  • Question

  • I am not able to find anything to help me figure out the problem with my `specified cast is not valid` error. I am not very experienced with Fluent NHibernate, so any help or a point in the right direction would be amazing. I have been researching to find a solution, but it doesn't seem like there are any answers valid to what is going on with my code. This is currently keeping a customer from viewing one page in their site, so I need to figure it out now as opposed to tomorrow, when other programmers are around to help out. I can't run this in Visual Studio locally because it is showing that it works. Locally, I have it pointed to the same database that the live site is using, so I know it's not an error with the database. I have checked the values in the database and all the data looks to be fine. Any help is appreciated.

       [InvalidCastException: Specified cast is not valid.]
        (Object , Object[] , SetterCallback ) +2556
        NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) +80
    
        [PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of Showroom.Vehicle]
        NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) +207
        NHibernate.Engine.TwoPhaseLoad.InitializeEntity(Object entity, Boolean readOnly, ISessionImplementor session, PreLoadEvent preLoadEvent, PostLoadEvent postLoadEvent) +989
        NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session, Boolean readOnly) +441
        NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +1157
        NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +205
        NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) +425
        NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) +23
        NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters) +99
        NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters) +494
        NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results) +743
        NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results) +675
        NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters) +235
        NHibernate.Impl.ExpressionQueryImpl.List() +189
        NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery) +61
        NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) +262
        NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) +26
        Remotion.Linq.QueryableBase`1.GetEnumerator() +83
        System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +446
        System.Linq.Enumerable.ToList(IEnumerable`1 source) +80
        Repository.Showroom.VehicleRepository.GetAll(Int32 siteID, Boolean active) in \Repository\Showroom\VehicleRepository.cs:52
        Showroom.Catalog_Maintenance.VehicleGrid_NeedDataSource(Object source, GridNeedDataSourceEventArgs e) in \Showroom\Catalog-Maintenance.aspx.cs:36
        Telerik.Web.UI.RadGrid.OnNeedDataSource(GridNeedDataSourceEventArgs e) +191
        Telerik.Web.UI.RadGrid.AutoDataBind(GridRebindReason rebindReason) +109
        Telerik.Web.UI.RadGrid.OnLoad(EventArgs e) +224
        System.Web.UI.Control.LoadRecursive() +71
        System.Web.UI.Control.LoadRecursive() +190
        System.Web.UI.Control.LoadRecursive() +190
        System.Web.UI.Control.LoadRecursive() +190
        System.Web.UI.Control.LoadRecursive() +190
        System.Web.UI.Control.LoadRecursive() +190
        System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178

    Here is the code it is referencing:

    line 52 of VehicleRepository:

    return this.GetQuery().Where(x => x.Active == active && x.Site.ID == siteID).ToList();
        

    GetQuery looks like this:

    public IQueryable<T> GetQuery()
    {
        return this.Session.Query<T>(); 
    }  

    line 36 of Catalog-Maintenance:

    var vehicles = _VehicleRepository.GetAll(SiteID, true);

    here is my Vehicle Map

    public class VehicleMap : OrderedEntityClassMap<Vehicle, Int32>
        {
            public VehicleMap()
            {
                Schema("Showroom"); 
    
                Map(x => x.Name);
                Map(x => x.Make);
                Map(x => x.Model);
                Map(x => x.BodyStyle);
                Map(x => x.Engine);
                Map(x => x.DriveType);
                Map(x => x.Transmission);
                Map(x => x.StockNumber);
                Map(x => x.ExteriorColor);
                Map(x => x.InteriorColor);
                Map(x => x.Warranty);
                Map(x => x.Price);
                Map(x => x.Vin);
                Map(x => x.Mileage);
                Map(x => x.Year);
                Map(x => x.Description);
                Map(x => x.Options);
                Map(x => x.Specs);
                Map(x => x.Active);
                Map(x => x.DateCreated);
                Map(x => x.LastUpdated);
                References(x => x.Site, "SiteID").LazyLoad();
    
                HasMany(x => x.Photos)
                    .KeyColumn("VehicleID")
                    .Cascade.AllDeleteOrphan()
                    .LazyLoad()
                    .Inverse();
            }
        }

    Thursday, July 14, 2016 10:01 PM

Answers

  • Hi jamie.grinnell,

    Welcome to MSND forum.

    Since your issue is related to the third product. I would suggest you post your issue on here

    https://groups.google.com/forum/#!forum/fluent-nhibernate

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries,
    samples, and tools.

    Best Regards,

    Hart


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Proposed as answer by Hart Wang Monday, July 18, 2016 8:57 AM
    • Marked as answer by jamiedewamie Monday, July 18, 2016 1:13 PM
    Saturday, July 16, 2016 6:24 AM

All replies

  • It seems to me that you should be finding a Fluent NHibernate forum, like Google groups has one and others like Stack Overflow.
    Thursday, July 14, 2016 11:24 PM
  • Ok thanks. Stack overflow users wouldn't help me since they thought it was similar to a question that was already asked (wrong), but I will try Google groups. 
    Friday, July 15, 2016 4:48 PM
  • Hi jamie.grinnell,

    Welcome to MSND forum.

    Since your issue is related to the third product. I would suggest you post your issue on here

    https://groups.google.com/forum/#!forum/fluent-nhibernate

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries,
    samples, and tools.

    Best Regards,

    Hart


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Proposed as answer by Hart Wang Monday, July 18, 2016 8:57 AM
    • Marked as answer by jamiedewamie Monday, July 18, 2016 1:13 PM
    Saturday, July 16, 2016 6:24 AM
  • Ok thank you, I will. :)
    Monday, July 18, 2016 1:13 PM
  • Ok thank you, I will. :)

    Did I not tell you the same thing?????
    Monday, July 18, 2016 6:29 PM