Nhibernate绑定到DataGridView

时间:2012-02-05 10:54:14

标签: nhibernate datagridview

为什么没有关于NHibernateDataGridViewWinforms绑定的任何教程。只有我想用它? 我知道这是绑定到DataGridView的问题。但我制作CRUD时遇到问题。

我有数据库SQLite +表,带有映射:

 <class name="Employee" table="emplyees" lazy="true">
    <id name="id">
      <generator class="increment"></generator>
    </id>

    <property name="first_name" not-null="true"></property>
    <property name="last_name" not-null="true"></property>
    <property name="login" not-null="true"></property>
    <property name="sid"></property>         
</class>

有效的代码。

Employee new_employee =
    new Employee() { first_name = "test1", last_name = "test3", login = "login1" };
session.Save(new_employee);
session.Commit();

但是如果我绑定到DataGridView并使用grid来插入一个新行:

transaction = session.BeginTransaction();
employees = (from e in session.Linq<Employee>() select e).ToList<Employee>();
this.employeeBindingSource.DataSource = employees;


private void employeeDataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
Employee new_employee = (Employee)this.employeeBindingSource.Current;
session.Save(new_employee);
}

session.Commit()之后我收到错误:Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect).在日志中,我看到NH为该新行发送UPDATE。应该插入。

1 个答案:

答案 0 :(得分:0)

行被另一个事务更新或删除(或未保存的值映射不正确)

Employee可能有一个id,因此NHibernate“认为”它应该被更新,但是NHibernate没有找到它,所以NH事务认为Employee在其他一些事务中被删除了。

id属性具有值而不是默认值。确保new_employee.id = 0;