nhibernate says 'mapping exception was unhandled' no persister for: MyNH.Domain.User

Posted by mrblah on Stack Overflow See other posts from Stack Overflow or by mrblah
Published on 2009-08-16T17:33:31Z Indexed on 2010/05/30 6:52 UTC
Read the original article Hit count: 628

Hi, I am using nHibernate and fluent.

I created a User.cs:

public class User
    {

        public virtual int Id { get; set; }
        public virtual string Username { get; set; }
        public virtual string Password { get; set; }
        public virtual string Email { get; set; }
        public virtual DateTime DateCreated { get; set; }
        public virtual DateTime DateModified { get; set; }

    }

Then in my mappinds folder:

 public class UserMapping : ClassMap<User>
    {

        public UserMapping()
        {
            WithTable("ay_users");

            Not.LazyLoad();
            Id(x => x.Id).GeneratedBy.Identity();
            Map(x => x.Username).Not.Nullable().WithLengthOf(256);
            Map(x => x.Password).Not.Nullable().WithLengthOf(256);
            Map(x => x.Email).Not.Nullable().WithLengthOf(100);
            Map(x => x.DateCreated).Not.Nullable();
            Map(x => x.DateModified).Not.Nullable();
        }

    }

Using the repository pattern for the nhibernate blog:

 public class UserRepository : Repository<User>
    {

    }




 public class Repository<T> : IRepository<T>
    {

        public ISession Session
        {
            get 
            { 
                return SessionProvider.GetSession(); 
            }
        }


        public T GetById(int id)
        {
            return Session.Get<T>(id);
        }

        public ICollection<T> FindAll()
        {
            return Session.CreateCriteria(typeof(T)).List<T>();
        }

        public void Add(T product)
        {
            Session.Save(product);
        }

        public void Remove(T product)
        {
            Session.Delete(product);
        }
    }



  public interface IRepository<T>
    {
        T GetById(int id);
        ICollection<T> FindAll();
        void Add(T entity);
        void Remove(T entity);
    }



 public class SessionProvider
    {
        private static Configuration configuration;
        private static ISessionFactory sessionFactory;

        public static Configuration Configuration
        {
            get
            {
                if (configuration == null)
                {
                    configuration = new Configuration();
                    configuration.Configure();                          
                    configuration.AddAssembly(typeof(User).Assembly);   
                }
                return configuration;
            }
        }

        public static ISessionFactory SessionFactory
        {
            get
            {
                if (sessionFactory == null)
                    sessionFactory = Configuration.BuildSessionFactory();
                return sessionFactory;
            }
        }

        private SessionProvider()
        { }

        public static ISession GetSession()
        {
            return SessionFactory.OpenSession();
        }
    }

My config:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Server=.\SqlExpress;Initial Catalog=TestNH;User Id=dev;Password=123</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

I created a console application to test the output:

static void Main(string[] args) {

        Console.WriteLine("starting...");


        UserRepository users = new UserRepository();

        User user = users.GetById(1);

        Console.WriteLine("user is null: " + (null == user));

        if(null != user)
            Console.WriteLine("User: " + user.Username);


        Console.WriteLine("ending...");
        Console.ReadLine();

    }

Error:

nhibernate says 'mapping exception was unhandled' no persister for: MyNH.Domain.User

What could be the issue, I did do the mapping?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about fluent-nhibernate