Database access problem by a module loaded with "referenced module" loader

Topics: Writing modules
Apr 16, 2012 at 11:30 AM

Hi Orchard Team,

I have a problem accessing to db after migration from my module, after its .dll was put into "~/bin" folder.

This didn't happen, when Orchard was using "dynamic module" loader, when it was compiled dynamically using .csproj file.

The migrations modify the database properly:

            SchemaBuilder.AlterTable("BasketItemPartRecord",
                table =>
                {
                    table.DropColumn("Added"); // was string
                    table.AddColumn<DateTime>("Added");
                }
            );

This is the model:

    public class BasketItemPart : ContentPart<BasketItemPartRecord>

...

        public long ItemID
        {
            get { return Record.ItemID; }
            set { Record.ItemID = value; }
        }

        public string SessionID
        {
            get { return Record.SessionID; }
            set { Record.SessionID = value; }
        }
        public DateTime Added
        {
            get { return Record.Added; }
            set { Record.Added = value; }
        }
public class BasketItemPartRecord : ContentPartRecord

...

public virtual long ItemID { get; set; }
public virtual string SessionID { get; set; }
public virtual DateTime Added { get; set; }

The data can be added to the database, but couldn't be read. With at least 1 row existing in database, the following exception is thrown by this code:

            string result = string.Empty;

            IEnumerable<BasketItemPart> items =_orchardServices.ContentManager.Query<BasketItemPart, BasketItemPartRecord>().List();

            return items.Aggregate(result, (current, item) => string.Format("{0}\nItemId:{1}, SessionId:{2}", current, item.ItemID, item.SessionID));

Exception:

Specified cast is not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[InvalidCastException: Specified cast is not valid.]
   (Object , Object[] , SetterCallback ) +1146
   NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(Object target, Object[] values) +20
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) +55

[PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of Basket.Models.BasketItemPartRecord]
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) +122
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValues(Object entity, Object[] values) +49
   NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values, EntityMode entityMode) +47
   NHibernate.Engine.TwoPhaseLoad.InitializeEntity(Object entity, Boolean readOnly, ISessionImplementor session, PreLoadEvent preLoadEvent, PostLoadEvent postLoadEvent) +505
   NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session, Boolean readOnly) +271
   NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +748
   NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +70
   NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) +125
   NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) +18
   NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes) +79
   NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) +557
   NHibernate.Impl.CriteriaImpl.List(IList results) +42
   NHibernate.Impl.CriteriaImpl.List() +58
   Orchard.ContentManagement.DefaultContentQuery.Slice(Int32 skip, Int32 count) in c:\Users\sebros\Desktop\1.3.10\src\Orchard\ContentManagement\DefaultContentQuery.cs:139
   Orchard.ContentManagement.ContentQuery`1.Orchard.ContentManagement.IContentQuery<T>.List() in c:\Users\sebros\Desktop\1.3.10\src\Orchard\ContentManagement\DefaultContentQuery.cs:184
   Basket.Controllers.TestController.Run() +86
   lambda_method(Closure , ControllerBase , Object[] ) +62
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   Orchard.Mvc.Routes.HttpAsyncHandler.EndProcessRequest(IAsyncResult result) in c:\Users\sebros\Desktop\1.3.10\src\Orchard\Mvc\Routes\ShellRoute.cs:148
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8969117
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Developer
Apr 16, 2012 at 12:02 PM

Please make sure that the ItemID column is Bigint. If you already have that, perhaps try to make your "Added" property (both Record and Part classes) of type DateTime?.

Apr 16, 2012 at 12:56 PM

No, this didn't help.

Coordinator
Apr 16, 2012 at 7:16 PM

Maybe your mappings file is stuck on the old schema for some reason. It can be found in app_data. Try to delete it and restart the app.