简体   繁体   English

NHibernate 3.3映射错误

[英]Wrong mapping NHibernate 3.3

Do you know where is problem with mapping file? 您知道映射文件在哪里吗?

Eroor: Could not compile the mapping document: NHibernateTutorial.Mapping.Character.hbm.xml Eroor:无法编译映射文档:NHibernateTutorial.Mapping.Character.hbm.xml

I Add all my files. 我添加所有文件。

Character 字符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NHibernateTutorial.Domain
{
    public class Character
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual int HealthPoints { get; set; }
        public virtual int Mana { get; set; }
        public virtual string Profession { get; set; }
    }
}

Mapping ( Character.hbm.xml ) 映射( Character.hbm.xml

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

  <class name="Character">
    <id name="Id">
      <generator class="guid" />
    </id>
    <property name="Name" />
    <property name="HealthPoints" />
    <property name="Mana" />
    <property name="Profession" />
  </class>

</hibernate-mapping>

Error 错误

在此处输入图片说明

ConnectionString 连接字符串

<?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.MsSql2012Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Data Source=RAFAL-KOMPUTER\MSSQLSERVER4;Database=rafal;Trusted_Connection=True;</property>
  </session-factory>
</hibernate-configuration>

NHibernateHelper NHibernateHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernateTutorial.Domain;

namespace NHibernateTutorial
{
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    var configuration = new Configuration();
                    configuration.Configure();
                    configuration.AddAssembly(typeof(Character).Assembly);
                    _sessionFactory = configuration.BuildSessionFactory();
                }
                return _sessionFactory;
            }
        }

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

    }
}

Character Repository 字符库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernateTutorial.Domain;
using NHibernate;

namespace NHibernateTutorial
{
    public class CharacterRepository
    {
        public void Add(Character newCharacter)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(newCharacter);
                    transaction.Commit();
                }
            }
        }

        public Character GetCharacterByName(string name)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                var result = session.QueryOver<Character>().Where(x => x.Name == name).SingleOrDefault();
                return result ?? new Character();
            }
        }

        public void Update(Character newCharacter)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(newCharacter);
                    transaction.Commit();
                }
            }
        }

        public void Delete(Character newCharacter)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(newCharacter);
                    transaction.Commit();
                }
            }
        }

    }
}

Exception Details: 异常详细信息: 在此处输入图片说明

See your exception details: 查看您的异常详细信息:

Could not instantiate dialect class NHibernate.Dialect.MsSql2012Dialect [...] 无法实例化方言类NHibernate.Dialect.MsSql2012Dialect [...]

Your problem isn't with the mapping but how you specify the database server technology (RDBMS) dialect. 您的问题不是映射,而是如何指定数据库服务器技术(RDBMS)的方言。 This could be happening because various reasons: 这可能是由于各种原因而发生的:

  • You're trying to use a dialect not present in your downloaded NHibernate version: do you have the latest version (3.0, 3.1, 3.2, 3.3...?). 您正在尝试使用下载的NHibernate版本中不存在的方言: 您是否拥有最新版本(3.0、3.1、3.2、3.3 ...?)。

  • You're specifying the dialect in the wrong place or in a wrong way. 您在错误的地方或以错误的方式指定了方言。

Double-check your configuration and if you've the latest version of NHibernate! 仔细检查您的配置,如果您拥有最新版本的NHibernate!

您是否在VS的属性中将映射文件Character.hbm.xml标记为Embedded Resource?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM