[英]Using the same model for two different repository in NHibernate
I have a model class like this, 我有这样的模型类,
Class Student
{
private virtual int StudentId; --> id column
private virtual int StudentName;
}
I have two repositories say LabRepository & LibraryRepository both of which has a table for Student. 我有两个存储库说LabRepository和LibraryRepository ,它们都有一个Student表。
Problem: When i try to retrieve the Student object from LabRepository and use the same model to insert into LibraryRepository, it fails because the StudentId value is 1 when retireved from LabRepository and NHibernate will execute a Update query instead of Insert query when it sees the Id column as 1.The Update query fails because there is no Student record with ID as 1 in Library 问题:当我尝试从LabRepository检索Student对象并使用相同的模型插入LibraryRepository时,它失败,因为当从LabRepository中退出时,StudentId值为1,并且NHibernate在看到Id时将执行Update查询而不是Insert查询列为1.更新查询失败,因为库中没有ID为1的学生记录
I have just given a sample here, but my actual model class is even complex with many id columns composed inside it.Is there any way by which I take a model object from one repository and insert the same into other repository without worrying on the id columns? 我刚刚在这里给出了一个示例,但是我的实际模型类甚至很复杂,其中包含许多id列。我有任何方法可以从一个存储库中获取模型对象并将其插入到其他存储库中而无需担心id列?
it could be done when mapping Student twice with a explicitly stated EntityName other than the class name 当使用明确声明的EntityName而不是类名映射Student两次时,可以完成此操作
change the idgeneration to assigned and map the reference 将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");
}
then it should be possible to 那应该是可能的
lab.Student = library.Student;
session.SaveOrUpdate(lab);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.