很多很多朋友的表格第一个建议。 [英] Many To Many Friends table Code First suggestions.

查看:64
本文介绍了很多很多朋友的表格第一个建议。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF4 CTP4 Code First。

I am using EF4 CTP4 Code First.

我有很多关系我无法弄清楚如何编码。

I have a many to many relationship I can't figure out how to code.

我正在建立一个用户拥有"朋友"的系统。 当你分配一个朋友时,它是一个收件人关系。

我认为朋友们将在一个名为Friend的多对多表中被代表。

当用户登录时在,我将查询朋友表以获取用户的WebAccountID的所有朋友。

I am building a system where the user has "friends".  When you assign a friend, it is a recipricle relationship.
I think the Friends are going to be represented in a many to many table called Friend.
When the user logs in, I will query the Friend table to get all friends for the user's WebAccountID.

有人可以给我一些关于如何编码这一情况的提示吗?

Could someone give me some tips on how to Code First this situation?

谢谢。



公共类WebAccount

{

    ; public int ID {get;组; }
    public string UserName {get;组; }
    ... ...
  // public virtual ICollection< Friend>朋友们{得到;组; }
   公共虚拟ICollection< WebAcct>朋友们{得到;组; }
}


public class WebAccount
{
    public int ID { get; set; }
    public string UserName { get; set; }
    ...
  //public virtual ICollection<Friend> Friends { get; set; }
    public virtual ICollection<WebAcct> Friends { get; set; }
}



公共级朋友

{

  ;&NBSP;&NBSP; public int ID {get;组; }
    public int WebAccountID {get;组; }
    public int FriendID {get;组; } //这是一个webaccountID以及
       

}


public class Friend
{
    public int ID { get; set; }
    public int WebAccountID { get; set; }
    public int FriendID { get; set; } //this is a webaccountID as well
       
}



           朋友fr =新朋友();

            fr.WebAcctID = 1;

            fr.FriendID = 2;

            db.Friends.Add(fr);


            Friend fr = new Friend();
            fr.WebAcctID = 1;
            fr.FriendID = 2;
            db.Friends.Add(fr);

            fr = new Friend();

            fr.WebAcctID = 2;

            fr.FriendID = 1;

            db.Friends.Add(fr);

            fr = new Friend();
            fr.WebAcctID = 2;
            fr.FriendID = 1;
            db.Friends.Add(fr);

            fr = new Friend();

            fr.WebAcctID = 1;

            fr.FriendID = 3;

            db.Friends.Add(fr);

            fr = new Friend();
            fr.WebAcctID = 1;
            fr.FriendID = 3;
            db.Friends.Add(fr);

            fr = new Friend();

            fr.WebAcctID = 3;

            fr.FriendID = 1;

            db.Friends.Add(fr);

            fr = new Friend();
            fr.WebAcctID = 3;
            fr.FriendID = 1;
            db.Friends.Add(fr);

            ////////////////////////////
¥b $ b        &NBSP;&NBSP;&NBSP;&NBSP; fr = new Friend();

            fr.WebAcctID = 4;

            fr.FriendID = 5;

            db.Friends.Add(fr);

            ////////////////////////////
            fr = new Friend();
            fr.WebAcctID = 4;
            fr.FriendID = 5;
            db.Friends.Add(fr);

            fr = new Friend();

            fr.WebAcctID = 5;

            fr.FriendID = 4;

            db.Friends.Add(fr);

            fr = new Friend();
            fr.WebAcctID =5;
            fr.FriendID = 4;
            db.Friends.Add(fr);

            fr = new Friend();

            fr.WebAcctID = 4;

            fr.FriendID = 6;

            db.Friends.Add(fr);

            fr = new Friend();
            fr.WebAcctID = 4;
            fr.FriendID = 6;
            db.Friends.Add(fr);

            fr = new Friend();

            fr.WebAcctID = 6;

            fr.FriendID = 4;

            db.Friends.Add(fr);

            fr = new Friend();
            fr.WebAcctID = 6;
            fr.FriendID = 4;
            db.Friends.Add(fr);

 

推荐答案

Terrence,

 

你可能想尝试这样的事情:

 

   
public class
WebAccount

    public class WebAccount

   
{

    {

       
public int ID {
获得; set ; }

        public int ID { get; set; }

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public string UserName {
获得; set ; }

        public string UserName { get; set; }

 

   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public virtual
ICollection < WebAccount > IAmFriendsWith {
get ; set ; }

        public virtual ICollection<WebAccount> IAmFriendsWith { get; set; }

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public virtual
ICollection < WebAccount > AreFirendsWithMe {
get ; set ; }

        public virtual ICollection<WebAccount> AreFirendsWithMe { get; set; }

 

   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
public void AddFriend( WebAccount 朋友)

        public void AddFriend(WebAccount friend)

       
{

        {

           
IAmFriendsWith.Add(朋友);

            IAmFriendsWith.Add(friend);

           
AreFirendsWithMe.Add(朋友);

            AreFirendsWithMe.Add(friend);

       
}

        }

   
}

    }

 

在这种情况下,我在WebAccounts之间创建了一个多对多的自引用关联。 
然而,这不是EF中的互惠关系,这意味着我不是因为他们是我的朋友而自动与某人交朋友。 
但是,这可以通过始终向两个集合中添加新朋友来模拟。 
然后,这应该使两个集合在关系的两边保持同步。

In this case I am creating a many-to-many self-referencing association between WebAccounts.  However, this is not a reciprocal relationship in EF, meaning that I am not automatically friends with somebody just because they are friends with me.  However, this can be simulated by always adding a new friend to both collections.  This should then keep both collections in sync for friends on both sides of the relationships.

 

希望这会有所帮助。

 

谢谢,

Arthur


这篇关于很多很多朋友的表格第一个建议。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆