使用错误(棕地)数据库模式映射一对一关系
我必须在 NHibernate 中对这种关系进行建模(稍微简化了代码以保持主题) - 员工可以是客户经理(因此,它是可选的):
table Employee (Id, Number, Name) 表 EmployeeIsAccountManager (Number, MaxAllowedDiscount)
不是在表 EmployeeIsAccountManager 中有一个指向表 Employee 的 Id 外键,而是在 Employee 表中有一个 Number 列,它指向表 EmployeeIsAccountManager 中的 Number 列。
我如何在 NHibernate 中映射它?我尝试在 EmployeeIsAccountManager 类映射上使用外部生成器,但如果我使用 Number 作为外部生成值,它会映射到 Employee 的 ID,即 Id 而不是 Number。我对我的类进行了建模以使用组合:
public class Employee
{
public virtual int Id { get; set; }
public virtual short Number {get; set; }
public virtual string Name {get; set; }
public virtual AccountManager AccountManager { get; set; }
}
public class AccountManager
{
public virtual short Number { get; set; } /*needed because of ID only?*/
public virtual decimal MaxAllowedDiscount { get; set }
}
我已经尝试了很多(一对一、多对一、外部生成器),但我不知道这是否可以用 NHibernate 来完成。顺便说一句:我可以更改类、映射等,但我无法更改表结构,因为它处于棕地状态(具有 2+ 百万行代码、近 1000 个表单的旧应用程序)。
任何帮助表示赞赏,谢谢! 特德
I have to model this relationship in NHibernate (simplified the code a bit to stay on-topic) - an employee can be an accountmanager (so, it's optional):
table Employee (Id, Number, Name)
table EmployeeIsAccountManager (Number, MaxAllowedDiscount)
instead of having an Id foreign key in table EmployeeIsAccountManager pointing to table Employee, I have a Number column in the Employee table which points to the Number column in table EmployeeIsAccountManager.
How do I map this in NHibernate? I've tried using the foreign generator on the EmployeeIsAccountManager class mapping, but if I use Number as foreign generated value, it maps to the ID of Employee, which is Id instead of Number. I modeled my class to use composition:
public class Employee
{
public virtual int Id { get; set; }
public virtual short Number {get; set; }
public virtual string Name {get; set; }
public virtual AccountManager AccountManager { get; set; }
}
public class AccountManager
{
public virtual short Number { get; set; } /*needed because of ID only?*/
public virtual decimal MaxAllowedDiscount { get; set }
}
I've tried a lot (one-to-one, many-to-one, foreign generator) but I can't figure out if this can be done with NHibernate. btw: I can change the classes, mappings, etc but I CANNOT change the table structure because of it's brownfield status (old application with 2+ million lines of code, nearly 1000 forms).
Any help is appreciated, thanks!
Ted
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题让我思考类继承,你可以将你的类 AccountManager 映射为 Employee 的子类,然后你将能够做你想做的事情,我已经为你测试了一个映射,但是当你设计表时,这种方式不会解决您的需求,因为在映射中您必须注意两点:
我想到的最后一个选项是使用映射的 Employee 类中的属性
到带有公式的 AccountManager 表,您可以在其中指定自定义选择来获取所需的值我假设属性 MaxAllowedDiscount,但这也有一些限制,当您使用公式映射属性时,无法插入或更新此属性。
希望这有帮助
如果有问题请告诉我。
Your question make me think about class inheritance, you could map your class AccountManager as a subclass of Employee and then you will able to do what you want to do, I'v etested a mapping for you but as you designed tables that way does not resolve your needs because there are two points you have to notice in your mapping:
The last option which comes in my mind is to use a property in the Employee class mapped
to the AccountManager table with a formula where you can specify a custom select to obtain the value you need I assume the property MaxAllowedDiscount, but this too has some limitation, when you map a property with formula, this property cannot be inserted nor updated.
Hope this helps
lat me konw if there are questions.
或使用GenerateBy.Assinged()
or with GeneratedBy.Assinged()