0

I am dealing with a problem i cannot solve by myself. I also tried to find out a solution on the net, without success.

Here the details ...

Its about a complex data binding (in this case , 3 database tables).

Here the tables i have (abstraction), trying to modelate a Users/Groups association

Table 1 - tblUsers
------------------------------
field1: Id
field2: Username
field3: Password

Table 2 - tblGroups
------------------------------
field1: Id
field2: GroupName
field3: Description

Table 3 - tblUsers_Groups
------------------------------
field1: Id
field2: Id_tblUsers
field3: Id_tblGroups

This way a single user can belong to multiples groups (and vice)

I have been trying to create a WinForm, like the Master - Detail kind, where the Master Grid should show the Users in tblUsers, and ... according to what I select there, show in the Details Grid, the groups the users selected belongs to ... but using the info in tblGroups

Could someone please give me a hand on this problem?

4

1 回答 1

1

第一个表3应该没有和ID
使用Id_tblUsers、Id_tblGroups的复合键

在.NET中我这样做的方式是

    public class UserGroup : Object
    {   // NO editing just add remove
        public User User { get; private set; }
        public Group Group { get; private set; }
        public override bool Equals(Object obj)
        {
            //Check for null and compare run-time types.
            if (obj == null || !(obj is UserGroup)) return false;
            UserGroup item = (UserGroup)obj;
            return (User.ID == item.User.ID && Group.ID == item.Group.ID);
        }
        public override int GetHashCode() { return (User.ID & 0xffff) + (Group.ID << 16); }
        public UserGroup(User user, Group group)
        { User = user; Group = group; }
    }

创建 UsersGroups 的 HashSet 并将其传递给 ctor 中的 Group 和 User。
它只是对对象的引用,因此每个人都在引用相同的数据。
覆盖 GetHash 以提高效率和 Equals,因此 HashSet 中不能有重复的 UserGroup。

这是返回用户所在组的代码

public List<Group> Groups
{   get { return usersGroups.Where(x => x.User == this)
                            .Select(x => x.Group)
                            .OrderBy(y => y.Name)
                            .ToList(); } }

然后群里的妹子来回用户。

它可能看起来很复杂,但很酷的是一个单一的主人。因此,当您修改 UserGroup 的 HashSet 时,它会同时反映在 User 和 Group 中

于 2012-10-16T18:02:22.307 回答