![](/img/trans.png)
[英]Issue with Scope_identity() when used with TransactionScope()
[英]SCOPE_IDENTITY when inserting values
尝试执行我的ORM程序时收到此错误消息。
NHibernate.Exceptions.GenericADOException:'无法插入:[NHibernateWinFormsApp.Employee] [SQL:插入员工默认值; 选择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);
有谁知道如何解决这个小问题?
看来您尚未映射某些在两个记录之间必须具有不同值的列。 因此,它们总是以null
插入,但您只能有一个这样的雇员,从而导致所有其他插入操作失败。
要么删除该约束,要么放宽它(通过使用过滤后的唯一索引忽略null
值来替换它),或者将这些列映射到某些属性,并为每位员工提供唯一的值。
但是,麻烦的底线是:始终查阅所有InnerException
及其特定属性(如果有)。 那不是NHibernate的特定建议,您应该在遇到所有异常时都这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.