Nhibernate - 无法加载类型/无法映射hbm文件

时间:2015-07-03 15:45:36

标签: c# nhibernate

我只是刚刚用NHibernate弄湿了,我已经走到了尽头......这是我非常简单的代码集。我的实体:

namespace NHibernate
{
    internal class Customer
    {
        public virtual int Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
    }
}

我的映射文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                  assembly="NHibernate"
                  namespace="NHibernate">
  <class name="Customer">
    <id name="Id">
      <generator class="native"></generator>
    </id>
    <property name="FirstName"></property>
    <property name="LastName"></property>
  </class>
</hibernate-mapping>

和我主要的电话:

namespace NHibernate
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var cfg = new Configuration();
            cfg.DataBaseIntegration(x =>
            {
                x.ConnectionString = "server=DEVPC2;Database=NHibernate;uid=demo;pwd=123;";
                x.Driver<SqlClientDriver>();
                x.Dialect<MsSql2012Dialect>();
            });

            var currentAssembly = Assembly.GetExecutingAssembly();
            var type = currentAssembly.GetType("NHibernate.Customer");
            cfg.AddAssembly(Assembly.GetExecutingAssembly());
            var sessionFactory = cfg.BuildSessionFactory();
            using (var session = sessionFactory.OpenSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    var customers = session.CreateCriteria<Customer>()
                        .List<Customer>();

                    foreach (var customer in customers)
                    {
                        Console.WriteLine("{0} {1}", customer.FirstName, customer.LastName);
                    }
                    tx.Commit();
                }
            }
            Console.ReadLine();
        }
    }
}

我的外部异常:

Could not compile the mapping document: NHibernate.Customer.hbm.xml

我的内心异常:

{"Could not load type 'NHibernate.Customer' from assembly 'NHibernate, Version=4.0.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4'.":"NHibernate.Customer"}

我已经手动尝试通过反射加载类型并且它有效......我真的很茫然。任何帮助表示赞赏:)

0 个答案:

没有答案