Fluent Nhibernate 外键映射

发布于 2024-12-20 22:07:12 字数 480 浏览 3 评论 0原文

我有桌子:

玩家
Id : int - 主键
名称:字符串

BowlerType
Id : int - 主键
描述:字符串

PlayerBowlerType
PlayerId :引用 Player.Id 的 int 不为 null 外键
BowlerTypeId : int not null 引用 BowlerType.Id 的外键

玩家可以确认多种保龄球类型。这是一些示例数据

玩家
1 |彼得
2 |约翰

BowlerType
6 |慢
7 |快速

PlayerBowlerType
1 | 6
1 | 7
2 | 7

I have the tables:

Player
Id : int - primarykey
Name : string

BowlerType
Id : int - primarykey
Description : string

PlayerBowlerType
PlayerId : int not null foreign key referencing Player.Id
BowlerTypeId : int not null foreign key referencing BowlerType.Id

A player can confirm to many bowling types. heres some example data

Player
1 | Peter
2 | John

BowlerType
6 | Slow
7 | Fast

PlayerBowlerType
1 | 6
1 | 7
2 | 7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

单身情人 2024-12-27 22:07:12

这里您需要的是一个与您的 PlayerBowlerType 一起使用的复合 id。此设置应该有效:

public class PlayerBowlerTypeId
{
    public virtual int PlayerId { get; set; }

    public virtual int BowlerTypeId { get; set; }

    public override bool Equals(object obj)
    {
        return Equals(obj as PlayerBowlerTypeId);
    }

    private bool Equals(PlayerBowlerTypeId other)
    {
        if (ReferenceEquals(other, null)) return false;
        if (ReferenceEquals(this, other)) return true;

        return PlayerId == other.PlayerId &&
            BowlerTypeId == other.BowlerTypeId;
    }

    public override int GetHashCode()
    {
        unchecked 
        {
            int hash = GetType().GetHashCode();
            hash = (hash * 31) ^ PlayerId.GetHashCode();
            hash = (hash * 31) ^ BowlerTypeId.GetHashCode();

            return hash;
        }
    }
}

public class PlayerBowlerType
{
    public PlayerBowlerType()
    {
        Id = new PlayerBowlerTypeId();
    }

    public virtual PlayerBowlerTypeId Id { get; set; }
}

public class PlayerBowlerTypeMap : ClassMap<PlayerBowlerType>
{
    public PlayerBowlerTypeMap()
    {
        Table("TABLENAME");

        CompositeId<PlayerBowlerTypeId>(x => x.Id)
            .KeyProperty(x => x.BowlerTypeId, "COLUMNNAME")
            .KeyProperty(x => x.PlayerId, "COLUMNNAME");
    }
}

从技术上讲,您可以在没有身份对象的情况下执行此操作(将删除 PlayerBowlerTypeId 类型,并将代码直接放入 PlayerBowlerType 中并进行适当调整),但我遇到了许多问题(3-4 个单独的错误)这样做造成的。其中之一被讨论这里

虽然我讨厌更改域对象来弥补 ORM 系统中的错误,但如果您只使用 PlayerBowlerTypeId 类型,它将为您省去很多麻烦。

只要您修改映射以使用实际的表和列名称(以及您需要对特定设置的映射执行的任何其他操作),这就应该有效。

What you need here is a composite id to use with your PlayerBowlerType. This setup should work:

public class PlayerBowlerTypeId
{
    public virtual int PlayerId { get; set; }

    public virtual int BowlerTypeId { get; set; }

    public override bool Equals(object obj)
    {
        return Equals(obj as PlayerBowlerTypeId);
    }

    private bool Equals(PlayerBowlerTypeId other)
    {
        if (ReferenceEquals(other, null)) return false;
        if (ReferenceEquals(this, other)) return true;

        return PlayerId == other.PlayerId &&
            BowlerTypeId == other.BowlerTypeId;
    }

    public override int GetHashCode()
    {
        unchecked 
        {
            int hash = GetType().GetHashCode();
            hash = (hash * 31) ^ PlayerId.GetHashCode();
            hash = (hash * 31) ^ BowlerTypeId.GetHashCode();

            return hash;
        }
    }
}

public class PlayerBowlerType
{
    public PlayerBowlerType()
    {
        Id = new PlayerBowlerTypeId();
    }

    public virtual PlayerBowlerTypeId Id { get; set; }
}

public class PlayerBowlerTypeMap : ClassMap<PlayerBowlerType>
{
    public PlayerBowlerTypeMap()
    {
        Table("TABLENAME");

        CompositeId<PlayerBowlerTypeId>(x => x.Id)
            .KeyProperty(x => x.BowlerTypeId, "COLUMNNAME")
            .KeyProperty(x => x.PlayerId, "COLUMNNAME");
    }
}

You can technically do this without an identity object (the PlayerBowlerTypeId type would be removed and the code placed directly into the PlayerBowlerType and suitably adapted), but I've had a number of problems (3-4 separate bugs) caused by doing this. One of them is discussed here.

While I hate changing the domain objects to compensate for bugs in the ORM system, if you just use the PlayerBowlerTypeId type, it will save you a lot of headaches.

This should work as long as you modify the mapping to use your actual table and column names (and whatever else you need to do with the mapping for your particular setup).

十六岁半 2024-12-27 22:07:12

我认为我们可以使用 HasManytoMany。
根据您的要求,您必须创建一个包含玩家和投球手类型 ID 的表。这是多对多的关系。

如果您查看此网站: https://github.com/jagregory/ fluid-nhibernate/wiki/入门
商店和产品的映射与您想要的映射相同。
在此处输入图像描述

I think we can use HasManytoMany.
Based from your requirement, you would have to create a table that contains the ids of the player and bowler type. This has a many to many relationship.

If you would look at this site: https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started
The mapping for Store and Products is the same as your intended mapping.
enter image description here

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