MappingException: persistent class not known

Topics: Writing modules
Nov 8, 2013 at 1:56 PM
Edited Nov 8, 2013 at 2:07 PM
Hello everybody. I've started using Orchard 1 month ago, so maybe I'm missing something.
Here's the problem:
My module's model have a class (a POCO) that I've made for a non-content data and table.
From the MyClassRecord class I'm trying to create a relation to the orchard users class because I'd like to save the current Orchard user when saving a MyClassRecord object.
I expect to just refer the User class in my MyClassRecord to create a User_Id field in MyClassRecord table.

This is the class:
public class MyClassRecord 
    public virtual int Id { get; set; }
    public virtual string Title { get; set; }

    public virtual Orchard.Users.Models.UserPartRecord UserPartRecord { get; set; }

If I try create a data migration from the orchard> command prompt, I get the following error:

Error executing command "codegen datamigration MyClassRecord"

persistent class not known: Orchard.Users.Models.UserPartRecord

Exception Details: NHibernate.MappingException: persistent class not known: Orch

Stack Trace:

[MappingException: persistent class not known: Orchard.Users.Models.UserPartReco
at NHibernate.Cfg.Configuration.Mapping.GetIdentifierType(String className)
at NHibernate.Type.ManyToOneType.SqlTypes(IMapping mapping)
at NHibernate.Mapping.Column.GetSqlTypeCode(IMapping mapping)

[MappingException: Could not determine type for column UserPartRecord_id of type
NHibernate.Type.ManyToOneType: NHibernate.MappingException]
at NHibernate.Mapping.Column.GetSqlTypeCode(IMapping mapping)
at Orchard.Data.Migration.Generator.SchemaCommandGenerator.<GetCreateFeatureC
ommands>d__18.MoveNext() in K:\4EditorialBoard\Orchard.2\src\Orchard\Data\Migrat
ion\Generator\SchemaCommandGenerator.cs:line 97
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Orchard.CodeGeneration.Commands.CodeGenerationCommands.CreateDataMigration
(String featureName)

Please note:
  • I've created a reference to the Orchard.Users module of course
  • my module is builds perfectly
  • the module has been enabled through "feature enable MyModuleName" before trying to create the migration
  • I've tried to create the migration even before enabling my module, same result.
  • I've tried to recompile all the solution, same result
What I'm missing here?
There are some limitations creating foreign keys referencing Orchard tables?
Could you please help me?

Many thanks.
Nov 11, 2013 at 6:40 AM
Do you have a dependency to the Orchard.Users module from your module?
Nov 11, 2013 at 9:23 AM
Yes, the problem was the dependency.
I've just added a "Dependencies: Orchard.Users" in my Module.txt.
Now generating the datamigration class I didn't get any error and the tables have been created properly.

Thanks Bertrand.
Nov 12, 2013 at 2:05 PM
Hi Bertrand,
could you please help me to another (related) point?

I've managed to create MyClassRecord, referencing UserPartRecord so creating a foreign key field of name UserPartRecord_id,
expected to host my current user when creating a MyClassRecord object...

Now I'm using this code...
IUser usr = _orchardServices.WorkContext.CurrentUser;
to get the current user.

How can I get the current user's UserPartRecord, in order to assign it to the MyClassRecord instance before saving it ??

Many thanks again.
Nov 12, 2013 at 6:06 PM
Nov 14, 2013 at 3:50 PM
Thanks Bertrand and sorry for the late reply.