Domanda

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.

È stato utile?

Soluzione

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();
}

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top