N-N relationship

Topics: Troubleshooting
Jul 24, 2015 at 2:08 PM
So again I read the tutorial here, and I've run into an issue which seems to be Nhibernate related according to the error logs. I've googled the error and keep ending up being pointed back at the tutorial, which might mean that I've either missed something, or misunderstood something, or both (but I've only been trying to figure this stuff out for a week!)

My code - I have an Assessment Part
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;

namespace AssessmentList.Models
{
   public class AssessmentsPart:ContentPart<AssessmentsPartRecord> 
    {
        public IEnumerable<AssessmentsDetailRecord> Assessments
        {
            get
            {
                return Record.Assessments.Select(r => r.AssessmentDetailsRecord);
            }
        }
    }
}
that links to an Assessment Detail Record
using System;

namespace AssessmentList.Models
{
    public class AssessmentsDetailRecord
    {
        public virtual int AssessmentsDetailRecord_ID { get; set; }
        public virtual string SurveyName { get; set; }
        public virtual DateTime? DueDate { get; set; }
        public virtual DateTime? CompletedDate { get; set; }
        public virtual Boolean IsComplete { get; set; }

    }
}
with an AssessmentPartRecord
using System.Collections.Generic;
using Orchard.ContentManagement.Records;

namespace AssessmentList.Models
{
    public class AssessmentsPartRecord:ContentPartRecord 
    {
        public AssessmentsPartRecord()
        {
            Assessments = new List<ContentAssessmentDetailsRecord>();
        }

        public virtual IList<ContentAssessmentDetailsRecord> Assessments { get;  set; }
    }
}
and a ContentAssessmentDetailsRecord
namespace AssessmentList.Models
{
    public class ContentAssessmentDetailsRecord
    {
        public virtual int ContentAssessmentDetailsRecord_ID { get; set; }
        public virtual AssessmentsDetailRecord AssessmentDetailsRecord { get; set; }
        public virtual AssessmentsPartRecord AssessmentsPartRecord { get; set; }
    }
}
I've a migration file that looks like this
using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using AssessmentList.Models;
using Orchard.Data;

namespace AssessmentList
{
    public class Migrations : DataMigrationImpl
    {

        private readonly IRepository<AssessmentsDetailRecord> _assessmentDetailsRepository;

        private readonly IEnumerable<AssessmentsDetailRecord> _assessmentDetails =
            new List<AssessmentsDetailRecord>
            {
                new AssessmentsDetailRecord { AssessmentsDetailRecord_ID=1,SurveyName="Pre-Session Survey" },
                new AssessmentsDetailRecord { AssessmentsDetailRecord_ID=2,SurveyName="Mid-Session Survey" },
                new AssessmentsDetailRecord { AssessmentsDetailRecord_ID=3,SurveyName="After-Session Survey" },
            };


        public Migrations(IRepository<AssessmentsDetailRecord> AssessmentDetailsRepository)
        {
            _assessmentDetailsRepository = AssessmentDetailsRepository;
        }

        public int Create()
        {

            SchemaBuilder.CreateTable("UserRecord", table => table
                .ContentPartRecord()
                .Column("UserID", DbType.Int32)
                .Column("FirstName", DbType.String)
                .Column("LastName", DbType.String)
            );

            ContentDefinitionManager.AlterPartDefinition(
                typeof(UserPart).Name, cfg => cfg.Attachable());

            return 1;
        }

        public int UpdateFrom1()
        {
            SchemaBuilder.AlterTable("UserRecord", table => table.AddColumn("Favourite_Colour", DbType.String));
            return 2;
        }

        public int UpdateFrom2()
        {
            SchemaBuilder.CreateTable("AssessmentsPartRecord",
                table => table
                .ContentPartRecord()
                );

            SchemaBuilder.CreateTable("AssessmentDetailsRecord",
                table => table
                .Column<int>("AssessmentsDetailRecord_ID", column => column.PrimaryKey().Identity())
                .Column<string>("SurveyName")
                .Column<DateTime>("DueDate")
                .Column<DateTime>("CompletedDate")
                .Column<Boolean>("IsComplete")
                );

            SchemaBuilder.CreateTable("ContentAssessmentDetailsRecord",
                table => table
                .Column<int>("ContentAssessmentDetailsRecord_ID", column => column.PrimaryKey().Identity())
                .Column<int>("AssesmentPartRecord_Id")
                .Column<int>("AssessmentsDetailRecord_Id")
                );

            ContentDefinitionManager.AlterPartDefinition(
                "AssessmentsPart",
                builder => builder.Attachable());

            return 3;
        }

        public int UpdateFrom3()
        {
            if (_assessmentDetailsRepository == null)
            {
                throw new InvalidOperationException("Couldn't find Assessment Details repository.");
            }

            foreach (var assessmentDetail in _assessmentDetails)
            {
                _assessmentDetailsRepository.Create(assessmentDetail);
            }

            return 4;
        }
    }
}
When I build the project, it's fine, I don't see any errors, but starting it gives me a server error saying a resource can't be found. Both the debug and error logs show the same error
_" http://localhost:30321/OrchardLocal/ NHibernate.MappingException: Association references unmapped class: AssessmentList.Models.ContentAssessmentDetailsRecord
_

I tried the sample code from the tutorial and it works as intended, so I can't see what I've got wrong. Incidentally, If I get Orchard to show my users (not included because this is getting long) it works, the moment I try to add my new files into the models folder and include them in a migrations file it goes wrong.
Jul 27, 2015 at 3:17 PM
partially resolved this - I had a few typos in my code that weren't helping. Solving that meant Orchard would start up, notify me that there was a module that needed upgrading on going into the dashboard, but would fail when trying the upgrade...

Commenting out UpdateFrom3(), allowed the tables to be created, I then found another typo which meant it wasn't inserting records (who'd have thought it'd get that picky about Id and ID!)

Just need to get my partial views to render now ;)