Fluent NHibernate 映射问题
我在使用 Fluent NHibernate 映射数据库时遇到困难。我正在处理的数据库是棕地,并且一些关系实施得很差。
我想要做的映射由下表描述:
+----------+ +----------+ +---------------+ +-------------------+
| ToolA | | ToolB | | ToolPurpose | | ToolInstruction |
+==========+ +==========+ +===============+ +===================+
| ToolA_Id | | ToolB_Id | | Purpose_Id | | ToolInstruction_Id|
| Name | | Name | | ToolA_Id | | Purpose_Id |
+----------+ +----------+ | ToolB_Id | | Instruction |
| Purpose | +-------------------+
+---------------+
我想要做的是将表 ToolA 与表 ToolInstruction 映射以获得一个类,例如:
public class ToolA
{
public virtual int _ToolA_id { get; set; }
public virtual string _Name { get; set; }
public virtual string _Instruction { get; set; }
}
或者
public class ToolA
{
public virtual int _ToolA_id { get; set; }
public virtual string _Name { get; set; }
public virtual ToolInstruction _ToolInstruction{ get; set; }
}
public class ToolInstruction
{
public virtual string _Instruction { get; set; }
}
我认为给我带来困难的是ToolPurpose 表对于每个 ToolA 和 ToolB 条目都有一行,因此对于 ToolA 条目,ToolB_Id 为空,反之亦然。
例如:
+--------------------------------------------------+
|Purpose_Id | ToolA_Id | ToolB_Id | Purpose |
|===================================================
|1 | 1 | null | "purpose here" |
|2 | null | 1 | "purpose here" |
+--------------------------------------------------+
我尝试了几种方法,它们在使用 PersistenceSpecification.VerifyTheMappings() 方法时显示出工作的迹象,但在我尝试从备份数据库中提取数据时却没有,因为我开始遇到空引用异常。
我尝试将 ToolPurpose 映射为链接器类,并尝试使用 ToolA 映射类中的 Join 方法来映射它。我还交替使用 HasOne 和 HasMany 来看看哪个更合适。
我希望有人能够通过列出他们将如何做到这一点或者他们在我的处理方式方面发现任何问题来为我指明正确的方向。
I'm having difficulty mapping a database using Fluent NHibernate. The database I'm working on is brownfield and some of the relationships are poorly implemented.
The mapping I'm trying to do is described by the following tables:
+----------+ +----------+ +---------------+ +-------------------+
| ToolA | | ToolB | | ToolPurpose | | ToolInstruction |
+==========+ +==========+ +===============+ +===================+
| ToolA_Id | | ToolB_Id | | Purpose_Id | | ToolInstruction_Id|
| Name | | Name | | ToolA_Id | | Purpose_Id |
+----------+ +----------+ | ToolB_Id | | Instruction |
| Purpose | +-------------------+
+---------------+
What I'm looking to do is to map the table ToolA with the table ToolInstruction to get a class such as:
public class ToolA
{
public virtual int _ToolA_id { get; set; }
public virtual string _Name { get; set; }
public virtual string _Instruction { get; set; }
}
OR
public class ToolA
{
public virtual int _ToolA_id { get; set; }
public virtual string _Name { get; set; }
public virtual ToolInstruction _ToolInstruction{ get; set; }
}
public class ToolInstruction
{
public virtual string _Instruction { get; set; }
}
What I think is causing me difficulty is that the ToolPurpose table has a row for every ToolA and ToolB entry such that for a ToolA entry the ToolB_Id is null and vice versa.
For example:
+--------------------------------------------------+
|Purpose_Id | ToolA_Id | ToolB_Id | Purpose |
|===================================================
|1 | 1 | null | "purpose here" |
|2 | null | 1 | "purpose here" |
+--------------------------------------------------+
I've tried a few approaches to this and they have shown signs of working when using the PersistenceSpecification.VerifyTheMappings() method but not when I've tried pulling data from a backup database because I start running into null reference exceptions.
I've tried mapping ToolPurpose as a linker class and I've tried mapping it by using the Join method in the ToolA mapping class. I've also alternated between using HasOne and HasMany to see which was suitable.
I'm hoping someone can point me in the right direction by laying out how they would do it or if they see any problems in terms of how I'm approaching it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以用它来回答你的问题的一部分:
u can use this for part of the your question:
我正在工作,没有任何关于我正在映射的数据库的文档,事实证明这就是我遇到困难的地方。 ToolPurpose 到 ToolA 关系实际上是多对一关系,使得每个工具具有多个用途,并且 ToolPurpose 到 ToolInstruction 关系也是多对一关系,使得每个用途可以有多个与其相关的指令。
这意味着直接的 HasMany() <-> References() 映射可用于将 ToolA、ToolPurpose 和 ToolInstruction 表链接在一起。 ToolPurpose 表中的空值不会以这种方式影响映射。
我应该在我的 openbing 帖子中提到,我只是提取而不是更新或保存到数据库,因此我不需要表的完整映射,因此我不必映射 ToolB。
I'm working without any documentation for the database I'm mapping and it turns out this was where my difficulty arose. The ToolPurpose to ToolA relationship is actually a many to one relationship such that each tool has multiple purposes and the ToolPurpose to ToolInstruction relationship is also a many to one relationship such that each purpose can have multiple instructions related to it.
This means a straight forward HasMany() <-> References() mappings can be used to link the ToolA, ToolPurpose and ToolInstruction tables together. The null values in the ToolPurpose table do not affect the mappings in this way.
I should have mentioned in my openbing post that I'm just extracting and not updating or saving to the database so I do not need a complete mapping of the tables so I don't have to map ToolB.