Nhibernate:无法编译映射文档.hbm.xml

时间:2011-06-06 07:55:48

标签: asp.net nhibernate nhibernate-mapping

// xml文件

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

  <class name="Contact" table="tbl_contact">

    <id name="ContactId" column="contact_id" type="int">
      <generator class="identity"></generator>
    </id>

    <property name="FirstName"  column="first_name" type="String"/>
    <property name="LastName"   column="last_name"  type="String"/>
    <property name="Email"      column="email"      type="String"/>
    <property name="Telephone"  column="telephone"  type="String"/>
  </class>
</hibernate-mapping>

// web.config中

<configSections>

        <section
          name="nhibernate"
          type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    />
    </configSections>

    <nhibernate>
        <add
          key="hibernate.connection.provider"
          value="NHibernate.Connection.DriverConnectionProvider"
    />
        <add
          key="hibernate.dialect"
          value="NHibernate.Dialect.MsSql2000Dialect"
    />
        <add
          key="hibernate.connection.driver_class"
          value="NHibernate.Driver.SqlClientDriver"
    />
        <add
          key="hibernate.connection.connection_string"
          value="Data Source=myServer;Initial Catalog=myDatabase;Persist Security Info=True;User ID=myDbUsername;"
    />
    </nhibernate>

//保存按钮

    NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();

    config.AddAssembly("ContactDomain");

    NHibernate.ISessionFactory factory = config.BuildSessionFactory();

    NHibernate.ISession session = factory.OpenSession();

    NHibernate.ITransaction transaction = session.BeginTransaction();


    // create contact
    Contact contact = new Contact();
    contact.FirstName = txtFirstName.Text;
    contact.LastName = txtLastName.Text;
    contact.Email = txtEmail.Text;
    contact.Telephone = txtTelephone.Text;

    // Tell NHibernate that this object should be saved
    session.Save(contact);

    // commit all of the changes to the DB and close the ISession
    transaction.Commit();
    session.Close();

1 个答案:

答案 0 :(得分:0)

从外观来看,你的web.config配置不正确,所以请看下面的例子。

    <configSections>
        <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
    </configSections>

    <connectionStrings>
        <add name="db" connectionString="Server=...;Database=...;Trusted_Connection=SSPI;"/>
    </connectionStrings>

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>

    <property name="proxyfactory.factory_class">
        NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
    </property>

    <property name="dialect">
        NHibernate.Dialect.MsSql2008Dialect, NHibernate
    </property>

    <property name="connection.connection_string_name">db</property>

</session-factory>
</hibernate-configuration>