Search code examples
enumscastle-activerecord

Create an activerecord class that has a field that behaves like an enum (but is a reference to another activerecord class)


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)


Solution

  • 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.