简体   繁体   English

在NHibernate中为两个不同的存储库使用相同的模型

[英]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. 我有两个存储库说LabRepositoryLibraryRepository ,它们都有一个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.

相关问题 使用Nhibernate使用相同的存储库方法返回不同的Dto - Return different Dto using same repository method with Nhibernate 使用城堡ActiveRecord,当两个类具有相同的名称但名称空间不同时,我得到了NHibernate DuplicateMappingException - Using Castle ActiveRecord, I get an NHibernate DuplicateMappingException when two classes have the same name but different namespaces NHibernate DuplicateMappingException 当两个类具有相同的名称但不同的命名空间时 - NHibernate DuplicateMappingException when two classes have the same name but different namespaces 使用相同类型的对象(NHibernate,C#)访问不同的表 - Access different tables using the same type of object (NHibernate, C#) 在NHibernate中使用Merge()时出错:具有相同标识符的另一个对象 - Error when using Merge() in NHibernate: a different object with the same identifier 如何使用NHibernate离开联接两个不同的表 - How to left join two different tables using NHibernate Nhibernate架构-通用Nhibernate信息库,可服务于多种类型 - Nhibernate Architecture - Generic Nhibernate Repository to serve many different types NHibernate:相同的类型,不同的 ClassMap? - NHibernate: Same type, different ClassMaps? NHibernate 存储库 - NHibernate Repository 调用NHibernate ISession.get <T> (对象id)两次具有相同的id返回两个不同的实例 - Calling NHibernate ISession.get<T>(object id) twice with the same id returns two different instances
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM