Search code examples
c#asp.netnhibernatenhibernate-mappingorchardcms-1.10

How to forcefully refresh persister collection/mappings.bin of NHibernate when a new table/entity is added in a Orchard CMS based web app?


I created a new table via Migrations.cs file :

        // New table  - StaffAddress
        SchemaBuilder.CreateTable("StaffAddress",
           tb => tb
               .Column<int>("StaffAddressId", p => p.PrimaryKey().Identity())
               .Column<bool>("UseForFutureItems", p => p.NotNull())
               .Column<int>("AddressId", p => p.Nullable())
               .Column<int>("StaffId", p => p.Nullable())
           );

        // foreign key relations
        SchemaBuilder.CreateForeignKey("Fk_StaffAddress_Address", "StaffAddress",
            new string[] { "AddressId" }, "Address", new string[] { "AddressId" });

        SchemaBuilder.CreateForeignKey("Fk_StaffAddress_Staff", "StaffAddress",
            new string[] { "StaffId" }, "Staff", new string[] { "OrchardUserID" });

I have added a mapping for the same in PersistenceConfigurations.cs as below:

        config.Mappings(x => x.AutoMappings.Add(AutoMap.Source(CreateTypeSource<entity.StaffAddress>())
        .Override<entity.StaffAddress>(map =>
        {
            map.Table("StaffAddress");
            map.Id(m => m.StaffAddressId, "StaffAddressId");
            map.References(y => y.Staff, "StaffId");
            map.References(z => z.Address, "AddressId");
        })));

Then I created the entity and wrote the code to save a StaffAddress and when I try to save one it is giving the following error :

2018-08-09 13:13:09,475 [22] Service - Default - Error on creating staff address .
NHibernate.MappingException: No persister for: Models.StaffSavedAddress
   at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
   at NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName, Object obj)
   at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.Save(Object obj)

Migration runs successfully and the table is created, the website is up but cannot save an address.

What I understand is mappings.bin is not getting updated or some persister collection in Orchard CMS/Nhibernate framework is not getting updated.

Any way to fix this issue?

NOTE: I can't delete the mappings.bin because the application is an automatic updating one.


Solution

  • I guess your mappings are not getting updated because the hash is not getting updated. So In the PersistenceConfigurations.cs check if you have updated the hash in the ComputingHash() method. If not, update the hash and your problem would be solved.