简体   繁体   English

ADO.net-复杂数据绑定

[英]ADO.net - Complex Data binding

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). 关于复杂的数据绑定(在本例中为3个数据库表)。

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 我一直在尝试创建一种WinForm,例如Master-Detail类型,在其中Master Grid应该在tblUsers中显示Users,然后...根据我在那里选择的内容,在Details Grid中显示所选用户所属的组...但是使用tblGroups中的信息

Could someone please give me a hand on this problem? 有人可以帮我解决这个问题吗?

First table 3 should not have and ID 第一个表3应该没有ID
Use a composite key of Id_tblUsers, Id_tblGroups 使用Id_tblUsers,Id_tblGroups的组合键

In .NET the way I do it is 在.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; }
    }

Create a HashSet of UsersGroups and pass it to both Group and User in the ctor. 创建一个UsersGroups HashSet并将其传递给ctor中的Group和User。
It is just a reference to an object so everyone is referencing the same data. 它只是对对象的引用,因此每个人都在引用相同的数据。
Override GetHash for efficiency and Equals so cannot have a duplicate UserGroup in the HashSet. 覆盖GetHash以获得效率和等于,因此HashSet中不能有重复的UserGroup。

The is the code for returning the groups a user is in 是返回用户所在组的代码

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

Then the sister in Group to return users. 然后在组中的姐姐返回用户。

It may seem complex but what is cool is a single master. 看起来似乎很复杂,但是一个大师是一件很酷的事情。 So when you revise HashSet of UserGroup it is reflected in both User and Group 因此,当您修改UserGroup的HashSet时,它将同时反映在User和Group中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM