DataMapper关联 - 组和成员

时间:2012-03-26 21:52:11

标签: c# ruby datamapper

Ruby(DataMapper ORM)中以下C#代码的等价物是什么?

class User{
    public long Id {get;set;}
}

class Group{
    public long Id {get;set;}
    public long Owner {get;set;} //User.Id
    public DbSet<User> Members {get;set;}
}

换句话说,我正在尝试使用所有者和成员来建模。我已经到达以下但我似乎无法使协会正常工作。

class User
    include DataMapper::Resource
    property :id, Serial
    has n, :groups
end

class Group
    include DataMapper::Resource
    property :id, Serial
    belongs_to :owner, 'User' 
    has n, :members, 'User' #something goofy going on here!
end

以上似乎强迫用户属于某个群组,但我希望它是可选的。

而FWIW,这是我正在尝试建模的SQL

create table users(
  id integer primary key autoincrement
, name text
);


create table groups(
  id integer primary key autoincrement
, name text unique
);


create table group_users(  
  group_id integer
, user_id integer
, role char check (role in ('u','o','a')) default 'u'
, primary key(group_id, user_id, role)
, foreign key (group_id) references groups(id)
, foreign key (user_id) references users(id)
);

TIA!

0 个答案:

没有答案