Nov 10, 2012 at 12:31 PM
Edited Nov 13, 2012 at 7:33 AM
|
Hi,
I run into the following Problem with Automapper:
I have two Classes CategoryRecord (DB Representation) and Category (like a ViewModel).
I tried to map these both with AutoMapper but I think I run into an Problem with lazy loading and nhibernate.
ViewModelClass:
public class Category
{
public int Id { get; set; }
[Required]
[Remote("CheckCategoryName", "Category", "Events")]
public string Name { get; set; }
public IList<Event> Events { get; set; }
}
DB Record Class
public class CategoryRecord
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
And the Profile for Automapper:
public MappingProfile()
{
Mapper.CreateMap<Category, CategoryRecord>();
}
Called with:
Mapper.AddProfile(new MappingProfile());
This Call gets the error:
[SqlCeException (0x80004005): The column name is not valid. [ Node name (if any) = events0_,Column name = Category_id ]]
System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr) +136
System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan() +798
System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) +363
System.Data.SqlServerCe.SqlCeCommand.ExecuteReader(CommandBehavior behavior) +59
System.Data.SqlServerCe.SqlCeCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader() +12
NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd) +279
NHibernate.Loader.Loader.GetResultSet(IDbCommand st, Boolean autoDiscoverTypes, Boolean callable, RowSelection selection, ISessionImplementor session) +255
NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +181
NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +126
NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type) +221
[GenericADOException: could not initialize a collection: [Rimango.Events.Models.CategoryRecord.Events#1][SQL: SELECT events0_.CategoryRecord_id as Category6_1_, events0_.Id as Id1_, events0_.Id as Id29_0_, events0_.Title as Title29_0_, events0_.Description as Descript3_29_0_, events0_.ImagePath as ImagePath29_0_, events0_.Category_id as Category5_29_0_ FROM Rimango_Events_EventRecord events0_ WHERE events0_.CategoryRecord_id=?]]
NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type) +329
NHibernate.Loader.Collection.CollectionLoader.Initialize(Object id, ISessionImplementor session) +27
NHibernate.Persister.Collection.AbstractCollectionPersister.Initialize(Object key, ISessionImplementor session) +29
NHibernate.Event.Default.DefaultInitializeCollectionEventListener.OnInitializeCollection(InitializeCollectionEvent event) +364
NHibernate.Impl.SessionImpl.InitializeCollection(IPersistentCollection collection, Boolean writing) +504
NHibernate.Collection.AbstractPersistentCollection.Initialize(Boolean writing) +46
NHibernate.Collection.AbstractPersistentCollection.Read() +12
NHibernate.Collection.PersistentBag.ToString() +15
System.String.Concat(Object arg0, Object arg1, Object arg2) +63
AutoMapper.AutoMapperMappingException.get_Message() +229
Orchard.Exceptions.DefaultExceptionPolicy.RaiseNotification(Exception exception) in c:\DEV\Orchard Projects\Pfarre.Anger1.6\src\Orchard\Exceptions\DefaultExceptionPolicy.cs:70
Orchard.Exceptions.DefaultExceptionPolicy.HandleException(Object sender, Exception exception) in c:\DEV\Orchard Projects\Pfarre.Anger1.6\src\Orchard\Exceptions\DefaultExceptionPolicy.cs:44
Orchard.Exceptions.Filters.UnhandledExceptionFilter.OnActionExecuted(ActionExecutedContext filterContext) in c:\DEV\Orchard Projects\Pfarre.Anger1.6\src\Orchard\Exceptions\Filters\UnhandledExceptionFilter.cs:38
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +239
System.Web.Mvc.<>c__DisplayClass15.b__12() +21
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +253
System.Web.Mvc.<>c__DisplayClass15.b__12() +21
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) +324
System.Web.Mvc.<>c__DisplayClass1d.b__19() +23
System.Web.Mvc.Async.<>c__DisplayClass1.b__0() +19
System.Web.Mvc.Async.<>c__DisplayClass8`1.b__7(IAsyncResult _) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c__DisplayClass8.b__3(IAsyncResult asyncResult) +25
System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
Orchard.Mvc.Routes.HttpAsyncHandler.EndProcessRequest(IAsyncResult result) in c:\DEV\Orchard Projects\Pfarre.Anger1.6\src\Orchard\Mvc\Routes\ShellRoute.cs:162
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629708
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
This appears in the LogFile:
2012-11-10 14:37:15,982 [95] NHibernate.Util.ADOExceptionReporter - The column name is not valid. [ Node name (if any) = events0_,Column name = Category_id ]
Any idea?
Update 13.11.2012:
Added correct CategoryRecord
Thanks
|
|
|
|
Ok, got an Solution.
Use
Mapper.CreateMap<CategoryRecord, Category>().ForMember(r => r.Events, m=>m.Condition((CategoryRecord c) => NHibernateUtil.IsInitialized(c.Events)));
to get rid of the exception. In my case this is enough because I don't need the Events. Otherwise I hope I can load these with NHibernateUtil.Initialize(object foo).
|
|
|
|
I dont remember exact syntax but you should just ignore c.Event in mapping definition of AutoMapper.
|
|
|
|
Thanks!
I found the problem and is was selfmade by me :).
I had an IList<EventRecord> on the CategoryRecord, but the Relationship from CategoryRecord and EventRecord is 1:n. In this case NHibernate is not able to map the event records to the categories.
I removed the IList and everything is working fine!
|
|