[英]Using the same model for two different repository in NHibernate
我有這樣的模型類,
Class Student
{
private virtual int StudentId; --> id column
private virtual int StudentName;
}
我有兩個存儲庫說LabRepository和LibraryRepository ,它們都有一個Student表。
問題:當我嘗試從LabRepository檢索Student對象並使用相同的模型插入LibraryRepository時,它失敗,因為當從LabRepository中退出時,StudentId值為1,並且NHibernate在看到Id時將執行Update查詢而不是Insert查詢列為1.更新查詢失敗,因為庫中沒有ID為1的學生記錄
我剛剛在這里給出了一個示例,但是我的實際模型類甚至很復雜,其中包含許多id列。我有任何方法可以從一個存儲庫中獲取模型對象並將其插入到其他存儲庫中而無需擔心id列?
當使用明確聲明的EntityName而不是類名映射Student兩次時,可以完成此操作
將neneration更改為已分配並映射引用
public LibraryStudentMap()
{
EntityName("LibraryStudent");
Table("LibraryStudents");
Id(x => x.StudentId).GeneratedBy.Assigned();
}
public LabStudentMap()
{
EntityName("LabStudent");
Table("LabStudents");
Id(x => x.StudentId).GeneratedBy.Assigned();
}
public LibraryMap()
{
References(x => x.Student).EntityName("LibraryStudent");
}
public LabMap()
{
References(x => x.Student).EntityName("LabStudent");
}
那應該是可能的
lab.Student = library.Student;
session.SaveOrUpdate(lab);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.