1

When I tried to perform CRUD operations on a POCO against a database, I got an exception: NHibernate Mapping Exception: No persister for: MyNamespace.Model.User.

Here is my code:

namespace MyNamespace.Model
{
    public interface IModel<TID>
    {
        TID ID { get; set; }
    }

    public class User : IModel<int>
    {
        public virtual int ID { get; set; }
        public virtual string Name { get; set; }
    }
}

and here is my mapping

namespace MyNamespace.Model.Mapping
{
    public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Table("User");
            Id(x => x.ID);
            Map(x => x.Name)
                .Length(255)
                .Unique()
                .Not.Nullable();
        }
    }
}

In my configuration file I have added the assembly mapping:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        ...
        <mapping assembly="MyNamespace" /> 
    </session-factory>
</hibernate-configuration>

I know that such issue occurs if using hbm files, when not set as embedded resources. But in my case I am using Fluent NHibernate where no such files are used. I also read here, that the Mapping classes should be public - I have done that as you can see. All my properties of the model class are virtual (to allow proxies to do their magic). I am absolutely unaware of what I am missing here and I'd be glad to hear any suggestions for this issue.

2 Answers 2

2

The <mapping> element is for hbm resources, not fluent mappings.

This is straight from the Fluent NHibernate docs:

private static ISessionFactory CreateSessionFactory()
{
  return Fluently.Configure()
    .Database(
      SQLiteConfiguration.Standard
        .UsingFile("firstProject.db")
    )
    .Mappings(m =>
      m.FluentMappings.AddFromAssemblyOf<Program>())
    .BuildSessionFactory();
}
Sign up to request clarification or add additional context in comments.

3 Comments

At the moment I cannot try your solution, but I am curious to why this might be the reason. I am configuring my session factory in the same old-fashioned way using the configuraton.BuildSessionFactory(). Maybe the mapping classes are not scanned taking into account the app.config settings? It is a little confusing having different ways to configure the same thing with the same information, and as result it works differently.
NHibernate does not know about Fluent. Therefore, NH's native config cannot possibly read Fluent mappings.
Thank you for the answer and clarifications. Now I realize that fluent maintains the mapping information in a completely different manner, probably incompatible with the hbm-dedicated storage. I just had assumed wrongly that Fluent would take into account assemblies passed in the app.config.
1

This error message most of the time means that NHibernate has no mappings for entity you are trying to save. So configure your session factory with FluentNHibernate to provide mappings for it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.