Fluent NHibernate - 升级 TPC 层次结构中的类
在给定的情况下,我有多个从基类继承的类,在每个具体类的表层次结构中,我可能需要从较低级别“升级”类到一个更高的水平。
作为一个例子,我将使用一个类 Employee
-> Manager
演示。
class Employee {
Guid Id { get; set; }
// certain properties
}
class Manager : Employee {
// certain unique properties
}
EmployeeMap : ClassMap<Employee> {
// mapping information
}
ManagerMap : SubClassmap<Manager> {
// appropriate unique properties mapping
}
var employee = new Employee {
Name = "Some Employee"
}
session.Save(employee);
那么,过了一会儿,Employee
被提升为 Manager
,那么现在我能做什么呢? dbo.Employees
和 dbo.Managers
是不同的表。如何从较低级别升级到较高级别而不丢失现有级别的所有内容?
In a given situation where I have multiple classes that inherit from a base class, in a Table Per Concrete Class
Hierarchy, I have a circumstance where it may be neccessary to 'upgrade' a class from a lower level to a higher level.
As an example, I will use a class Employee
-> Manager
demonstration.
class Employee {
Guid Id { get; set; }
// certain properties
}
class Manager : Employee {
// certain unique properties
}
EmployeeMap : ClassMap<Employee> {
// mapping information
}
ManagerMap : SubClassmap<Manager> {
// appropriate unique properties mapping
}
var employee = new Employee {
Name = "Some Employee"
}
session.Save(employee);
Now then, a while later, that Employee
gets bumped to Manager
, so now what can I do? dbo.Employees
and dbo.Managers
are different tables. How can I upgrade from a lower class to a higher one without losing everything attached to the existing one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,我想不出任何方法来巧妙地执行此更新 - 因为您正在使用每个具体类的表,我能想到的唯一方法是删除现有的员工并添加新的经理。
话虽如此,我确实有一些可以帮助你的东西——我出于不同的原因需要它,但它也可能对你的情况有帮助。
下面的类使用反射来提供通用的“复制”机制。要使用它,请尝试以下代码片段(假设员工是即将晋升的员工):
经理对象现在应该具有与员工对象相同的属性值(至少在两个类中都存在该属性的情况下)。现在您可以提交经理并删除原始员工。
PropertyCopier 类代码如下:
希望这有帮助!
Unfortunately, I can't think of any way to perform this update neatly - because you are using the Table Per Concrete Class, the only way I can think of is to remove the existing Employee and add a new Manager.
Having said that, I do have something that may help you - I needed it for a different reason, but it may help in your case too.
The class below uses reflection to provide a generic "copy" mechanism. To use it, try the following code snippet (assuming employee is the employee getting promoted):
The manager object should now have the same property values as the employee object did (at least where the property exists in both classes). Now you can commit the manager and delete the original employee.
The PropertyCopier class code is as follows:
Hope this helps!