创建一个 ActiveRecord 类,该类具有一个行为类似于枚举的字段(但是对另一个 ActiveRecord 类的引用)
抱歉标题很长,问题相当简单:
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您指定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.