创建一个 ActiveRecord 类,该类具有一个行为类似于枚举的字段(但是对另一个 ActiveRecord 类的引用)

发布于 2024-10-18 02:37:16 字数 508 浏览 1 评论 0原文

抱歉标题很长,问题相当简单:

我有 2 个类,玩家和角色(它们是 activerecord 表)

class Player
{
    ...Various fields...

    [BelongsTo("RoleId")]
    public Role Role {get;set;}
}

class Role
{
    ...Various fields...

    [Property]
    public string Name {get;set;}
}

一个玩家只能有一个角色,但是(对我来说),角色是否有 0- 并不重要1-2-很多玩家,所以我想省略 HasMany 属性(我的例子很简单,但我的数据库比这个大得多)。角色的行为类似于用户定义的枚举,可以这样做吗?哪个是正确的方法?

编辑1: 如果我有类似的情况,但我需要一个角色仅属于一个玩家(onetoone),但我想再次省略“角色”类中的部分(因此角色对这个关联一无所知)怎么办?

Sorry for long title, the question is rather simple:

I have 2 classes, Player and Role (they are activerecord table)

class Player
{
    ...Various fields...

    [BelongsTo("RoleId")]
    public Role Role {get;set;}
}

class Role
{
    ...Various fields...

    [Property]
    public string Name {get;set;}
}

A Player, can have only one role, but (for me), it doesn't matter if Role has 0-1-2-many players, so I would like to omit HasMany attribute (my example is easy, but my database is much bigger than this). Role is behaving like a user-defined enum, is possible to do this? Which is the correct way?

Edit 1:
What if I have a similar situation but I need that a Role belogs only to one player (onetoone), but again I would like to omit the part from the "Role" class (so role doesn't know anything about this association)

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

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

发布评论

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

评论(1

叹倦 2024-10-25 02:37:16

如果您指定BelongsTo属性而不是HasMany属性,我想要的已经完成,角色是否有0-1-2多个玩家并不重要。

If you specify BelongsTo attribute and not HasMany attribute, what I want is already done, the Role doesn't matter if has 0-1-2-many players.

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