Fluent NHibernate 映射问题

发布于 2024-12-02 08:53:40 字数 1900 浏览 2 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神魇的王 2024-12-09 08:53:40

你可以用它来回答你的问题的一部分:

ToolA
------
ToolBs = new Dictionary<string, ToolB>();
public virtual IDictionary<string, ToolB> ToolBs { get; set; }

HasManyToMany<ToolB>(x => x.ToolBs)
                .Table("ToolPurpose")
                .ChildKeyColumn("ToolB_Id ")
                .ParentKeyColumn("ToolA_Id")
                .AsMap<string>("Purpose")
                .Cascade.All();

u can use this for part of the your question:

ToolA
------
ToolBs = new Dictionary<string, ToolB>();
public virtual IDictionary<string, ToolB> ToolBs { get; set; }

HasManyToMany<ToolB>(x => x.ToolBs)
                .Table("ToolPurpose")
                .ChildKeyColumn("ToolB_Id ")
                .ParentKeyColumn("ToolA_Id")
                .AsMap<string>("Purpose")
                .Cascade.All();
温柔嚣张 2024-12-09 08:53:40

我正在工作,没有任何关于我正在映射的数据库的文档,事实证明这就是我遇到困难的地方。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文