尝试执行我的ORM程序时收到此错误消息。
NHibernate.Exceptions.GenericADOException:'无法插入: [NHibernateWinFormsApp.Employee] [SQL:INSERT INTO Employee DEFAULT 价值;选择SCOPE_IDENTITY()]'
这是我的地图文件编辑
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateWinFormsApp"
namespace="NHibernateWinFormsApp">
<class name="Employee">
<id name="EmployeeId" column="EmployeeId">
<column name="EmployeeId" not-null="true" />
<column name="FirstName" />
<column name="LastName" />
<generator class="native"/>
</id>
<bag name="Department" table="EmployeeDepartment" >
<key column="EmployeeId" not-null="true" />
<many-to-many class="Department" column="DepartmentId"/>
</bag>
</class>
<class name="Department">
<id name="DepartmentId">
<column name="DepartmentId" not-null="true" />
<column name="DepartmentName" />
<generator class="native"/>
</id>
<bag name="Employee" table="EmployeeDepartment" inverse="true">
<key column="DepartmentId" not-null="true"/>
<many-to-many class="Employee" column="EmployeeId"/>
</bag>
<property name="DepartmentName" type="string"/>
</class>
</hibernate-mapping>
而且,进入我的会议:
DepartmentRepository departmentRepository = new DepartmentRepository(session);
// get department by id
Department department = departmentRepository.GetById(int.Parse(textDepartment.Text));
employeeData.Department = new List<Department>();
employeeData.Department.Add(department);
department.Employee.Add(employeeData);
有谁知道如何解决这个小问题?
答案 0 :(得分:1)
看起来您没有映射某些列,这些列必须在两个记录之间具有不同的值。所以它们总是插入null
,但是你只能有一个这样的雇员,导致所有其他插入失败。
删除该约束,或者放宽它(通过将其替换为忽略null
值的过滤的唯一索引),或将这些列映射到某些属性,并为每个员工提供唯一值。
但是,你的麻烦的底线是:总是咨询所有InnerException
及其特定属性(如果有的话)。这不是NHibernate的具体建议,你应该在遇到的所有例外情况下这样做。