嘗試執行我的ORM程序時收到此錯誤消息。SCOPE_IDENTITY插入值時
NHibernate.Exceptions.GenericADOException:「無法插入: [NHibernateWinFormsApp.Employee] [SQL:INSERT INTO員工DEFAULT VALUES;選擇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);
有誰知道如何解決這個小問題?
可能重複[無法插入選擇範圍\ _IDENTITY()流利nhibernate一對多](https://stackoverflow.com/questions/25096975/could-not-insert-select-scope-identity-fluent-nhibernate一對多) –
@ahmedabdelqader我不確定我是否同意。請告訴我,如果我錯了。 :) – user7879148
這是完整的錯誤信息?對我來說,它看起來被截斷了。 SQL-Server應該告訴更多。檢查內部異常,尋求'SqlException'並查閱他們的'Errors'屬性。否則捕獲失敗的插入SQL並直接在你的數據庫上重放它。 –