nhibernate说'映射异常未处理'没有持久性:MyNH.Domain.User

时间:2009-08-16 17:33:32

标签: nhibernate fluent-nhibernate

我正在使用nHibernate并且流利。

我创建了一个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; }

    }

然后在我的mappinds文件夹中:

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

    }

使用nhibernate博客的存储库模式:

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

我的配置:

<?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>

我创建了一个控制台应用程序来测试输出:

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

    }

错误:

nhibernate说'映射异常未处理'没有持久性:MyNH.Domain.User

可能是什么问题,我做了映射?

1 个答案:

答案 0 :(得分:1)

NH表示你拥有User类的映射 你可能忘了把它包括在内。尝试从SessionProvider配置器中调用它:

Fluently.Configure()
  .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMapping>())
  .BuildSessionFactory();