This project is read-only.

NHibernate exception when no record found

Topics: Customizing Orchard, Troubleshooting, Writing modules
May 22, 2013 at 9:49 AM
I am writing a custom part to store settings for a new module.
The first time I display the settings screen, there are (of course) no records in the database, because I haven't entered any settings.

I am getting a SQL CE Exception
[SqlCeException (0x80004005): There was an error parsing the query. [ Token line number = 1,Token line offset = 81,Token in error = User ]]
with an underlying error of:
[GenericADOException: could not load an entity: [SSAT.Orchard.XrmCore.Models.XrmSettingsPartRecord#1][SQL: SELECT xrmsetting0_.Id as Id281_0_, xrmsetting0_.Url as Url281_0_, xrmsetting0_.User as User281_0_, xrmsetting0_.Password as Password281_0_ FROM SSAT_Orchard_XrmCore_XrmSettingsPartRecord xrmsetting0_ WHERE xrmsetting0_.Id=?]]
   NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, String optionalEntityName, Object optionalIdentifier, IEntityPersister persister) +374
From reading this this looks to me as if the Exception is being thrown because there is no record found. Is there any way to make this return a null object instead?

Part, record and migration codes below:
namespace SSAT.Orchard.XrmCore.Models
    public class XrmSettingsPart : ContentPart<XrmSettingsPartRecord>
        public string Url
            get { return Record.Url;}
            set { Record.Url = value; }

        public string User
            get { return Record.User; }
            set { Record.User = value; }

        public string Password
            get { return Record.Password; }
            set { Record.Password = value; }


    public class XrmSettingsPartRecord : ContentPartRecord
        public virtual string Url { get; set; }
        public virtual string User { get; set; }
        public virtual string Password { get; set; }

namespace SSAT.Orchard.XrmCore {
    public class Migrations : DataMigrationImpl {

        public int Create() {
            // Creating table XrmSettingsPartRecord
            SchemaBuilder.CreateTable("XrmSettingsPartRecord", table => table
                .Column("Url", DbType.String)
                .Column("User", DbType.String)
                .Column("Password", DbType.String)

            return 1;
Any pointers please?
May 22, 2013 at 10:46 AM
Edited May 22, 2013 at 10:48 AM
Orchard handles non-existent records just fine - the issue is most likely because you're using an SQL CE reserved word User as a column name. Try renaming the column.
May 22, 2013 at 1:04 PM
Sorted - thanks!

Obviously a weakness of the NHIbernate library but clearly that's not Orchard's fault...