【问题标题】:NHibernate Could not compile the mapping document using MySqlNHibernate 无法使用 MySql 编译映射文档
【发布时间】:2017-06-21 04:12:42
【问题描述】:

我目前正在尝试使用 NHibernate 连接到 MySQL 数据库。到目前为止,基本操作是在 UI 中向数据库添加新用户。目前我的代码如下所示:-

hibernate.cfg.xml

<?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="connection.driver_class">
            NHibernate.Driver.MySqlDataDriver
        </property>
        <property name="connection.connection_string">        
            Server=*********;Database=********;Username=*********;Password=*********;
        </property>
        <property name="dialect">
            NHibernate.Dialect.MySQLDialect
        </property>
        <property name="show_sql">
            true
        </property>
    </session-factory>
</hibernate-configuration>

User.cs

namespace APIDB.Domain
{
    class User
    {
        public virtual Guid Id { get; set; }
        public virtual string Username { get; set; }
        public virtual string Password { get; set; }
        public virtual bool LoggedIn { get; set; }
    }
}

User.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="WindowsFormsApplication1"
               namespace="WindowsFormsApplication1.Domain">

    <class name="User">
        <id name="Id">
            <column name="Id" />
            <generator class="guid" />
        </id>
        <property name="Username" column="Username" />
        <property name="Password" column="Password"/>
        <property name="LoggedIn" column="LoggedIn"/>
    </class>
</hibernate-mapping>

NHibernateHelper.cs

namespace APIDB
{
    class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;
        private static ISessionFactory SessionFactory
        {
            get
            {
                if(_sessionFactory == null)
                {
                    var cfg = new Configuration();
                    cfg.Configure();
                    cfg.AddAssembly(typeof(User).Assembly);
                    _sessionFactory = cfg.BuildSessionFactory();
                }
                return _sessionFactory;
            }
        }

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

DAO 添加方法

namespace APIDB
{
    class DAO
    {
        public void Add(User newUser)
        { 
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using(ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(newUser);
                    transaction.Commit();
                }
            }
        }
    }
}

在主界面调用

public void addClient(String username, String password)
{
    LoadHibernateCfg();
    DAO dao = new DAO();
    var tempUser = new User { Username = username, Password = password, LoggedIn = true };
    dao.Add(tempUser);
}

public static void LoadHibernateCfg()
{
    var cfg = new NHibernate.Cfg.Configuration();
    cfg.Configure();
    cfg.AddAssembly(typeof(User).Assembly);  //ERROR OCCURS ON THIS LINE
    new SchemaExport(cfg).Execute(true, true, false);
}

当我尝试运行此代码时,会抛出 MappingException 并收到消息:-

NHibernate Could not compile the mapping document: WindowsFormsApplication1.Mapping.User.hbm.xml

非常感谢任何帮助纠正此问题。

【问题讨论】:

    标签: c# mysql nhibernate


    【解决方案1】:

    看起来映射“WindowsFormsApplication1.Domain”中的命名空间与实际的域命名空间“APIDB.Domain”不同。程序集名称也可能有误,但我不知道为您的项目生成的程序集名称是什么。

    【讨论】:

    • 快速提问,我在哪里可以在我的项目中找到程序集名称?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 2015-04-26
    相关资源
    最近更新 更多