代码之家  ›  专栏  ›  技术社区  ›  user366312

开始使用NHibernate

  •  1
  • user366312  · 技术社区  · 16 年前

    我正在尝试在NHibernate开发我的Hello World程序。

    我的代码如下:

    MyClass.cs
    ----------
    using System.Collections.Generic;
    using System.Text;
    using System;
    using NHibernate.Collection;
    using NHibernate.Mapping;
    using Iesi.Collections;
    
    namespace NHibernate__MyClass
    {
        public class MyClass 
        {
            int id;
            string name;
            int _value;
    
            public MyClass()
            {
                id = 0;
                name = "";
                _value = 0;
            }
    
            public virtual int Id
            {
                get { return id; }
                set { id= value; }
            }
    
            public virtual string Name
            {
                get { return name; }
                set { name= value; }
            }
    
            public virtual int Value
            {
                get { return _value; }
                set { _value= value; }
            }
        }
    }
    
    
    MyClass.hbm.xml
    ---------------
    <?xml version="1.0" encoding="utf-8" ?>
    
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernate__MyClass" assembly="NHibernate__MyClass">
      <class name="MyClass" table="MyClass">
        <id name="Id">
          <column name="ID" sql-type="int" not-null="true"/>
          <generator class="native" />
        </id>
        <property name="Name">
          <column name="Name" not-null="true" />
        </property>
        <property name="Value">
          <column name="Value" not-null="true" />
        </property>
      </class>
    </hibernate-mapping>
    
    Program.cs
    ----------
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    using NHibernate;
    using NHibernate.Cfg;
    
    namespace NHibernate__MyClass
    {
        class Program
        {
            static void Main(string[] args)
            {
                MyClass myClass = new MyClass();
                myClass.Id = 1;
                myClass.Name = "Hello World!";
                myClass.Value = 100;
    
                ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
                ISession session = sessionFactory.OpenSession();
    
                session.BeginTransaction();
                session.Save(myClass);
                session.Transaction.Commit();
    
                Console.ReadLine();
            }
        }
    }
    
    App.config
    ----------
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section
        name="hibernate-configuration"
        type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
    />
      </configSections>
    
      <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory>
          <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
          <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
          <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=NHibernate;Integrated Security=True</property>
          <mapping assembly="NHibernate__MyClass" />
        </session-factory>
      </hibernate-configuration>
    </configuration>
    
    
    SQL Table
    ---------
    CREATE TABLE [dbo].[MyClass](
        [ID] [int] NOT NULL,
        [Name] [varchar](50) NOT NULL,
        [value] [int] NOT NULL
    ) ON [PRIMARY]
    

    但是这个程序正在生成一个异常:

    异常消息:“无法插入:[nhibernate_uumyclass.myclass][sql:插入myclass(name,value)值(??);选择范围_identity()]“

    内部异常消息:“无法将值空值插入列'id',表'nhibernate.dbo.myclass';列不允许空值。插入失败。\r\n语句已终止。“

    有什么问题?

    nhibernate dll版本=2.0.0.2002

    1 回复  |  直到 16 年前
        1
  •  3
  •   Spencer Ruport    16 年前

    因为映射文件中的这个标记

    <generator class="native" />
    

    在SQL中,需要将该表中的ID字段设置为标识。

    您也可以让NHibernate生成标识字段。