Database Windows Phone 7.1-同一表上的多对一关系-DB

Database Windows Phone 7.1-同一表上的多对一关系-DB,database,windows-phone-7,datatable,many-to-one,Database,Windows Phone 7,Datatable,Many To One,我正在尝试创建一个DB,它在对象与自身之间具有多对一关系。 (我有一个用户,该用户应该有他知道的其他用户的列表…) 我有两个问题: 我知道,为了在不同的表之间建立多对一关系,您需要在一侧创建一个entitySet,该entitySet应该有列表,在另一侧创建一个entityRef。 在这个过程中,有一件事我没有领会: 在“多”端的对象中,添加了一个id(它是“一”端的主键,也是“一”端类型的对象)。 为什么我需要这样做来创建一个对象?我不能只做关键点吗 代码示例: (一个国家/地区的城市-为

我正在尝试创建一个DB,它在对象与自身之间具有多对一关系。 (我有一个用户,该用户应该有他知道的其他用户的列表…)

我有两个问题:

  • 我知道,为了在不同的表之间建立多对一关系,您需要在一侧创建一个entitySet,该entitySet应该有列表,在另一侧创建一个entityRef。 在这个过程中,有一件事我没有领会: 在“多”端的对象中,添加了一个id(它是“一”端的主键,也是“一”端类型的对象)。 为什么我需要这样做来创建一个对象?我不能只做关键点吗
代码示例:

(一个国家/地区的城市-为什么在城市类中有一个国家/地区对象和一个国家/地区Id?)

[表格]
公营国家
{
私人实体设置citiesRef;
公共国家()
{
this.citiesRef=新实体集(this.OnCityAdded,this.OnCityRemoved);
}
[列(IsPrimaryKey=true,IsDbGenerated=true)]
公共整数ID
{
得到;
设置
}
[列(CanBeNull=false)]
公共字符串名
{
得到;
设置
}
[协会(Name=“FK\u Country\u Cities”,Storage=“citiesRef”,ThisKey=“ID”,OtherKey=“CountryID”)]
公共实体集城市
{
得到
{
返回此.citiesRef;
}
}
添加后的私人无效(城市)
{
city.Country=这个;
}
删除后的私人空间(城市)
{
city.Country=null;
}
}
[附表]
公营城市
{
私有可为空的countryID;
私有EntityRef countryRef=新EntityRef();
[列(IsPrimaryKey=true,IsDbGenerated=true)]
公共整数ID
{
得到;
设置
}
[列(CanBeNull=false)]
公共字符串名
{
得到;
设置
}
[列(Storage=“countryID”,DbType=“Int”)]
公共国际国家ID
{
得到
{
返回此.countryID;
}
设置
{
this.countryID=值;
}
}
[协会(Name=“FK\u Country\u Cities”、Storage=“countryRef”、ThisKey=“CountryID”、OtherKey=“ID”、IsForeignKey=true)]
公共国家
{
得到
{
返回此.countryRef.Entity;
}
设置
{
Country previousValue=this.countryRef.Entity;
if(((previousValue!=value)| |(this.countryRef.hasloadeOrassignedValue==false)))
{
如果((以前的值!=null))
{
this.countryRef.Entity=null;
previousValue.Cities.Remove(此);
}
this.countryRef.Entity=值;
如果((值!=null))
{
值。城市。添加(此);
this.countryID=value.ID;
}
其他的
{
this.countryID=默认值(可为空);
}
}
}
}
}
  • 如何创建所需的多对一关系(从用户到用户)? 我尝试了一些东西,但它并没有让我有任何进展(而且-我真的不明白我在那里做什么-因为基本上结果是每个用户都有一个id、friendId和一个friendList,我不想要朋友id

     [Table]
      public class User
      {
        [Column (IsPrimaryKey=true)]
        public int Id { get; set; }
        [Column]
        public string Name { get; set; }
        [Column]
        public string Gender { get; set; }
    
    private EntitySet<User> friends;//the list of users the user knows
    [Association(Storage = "friends", ThisKey = "Id", OtherKey = "MyFriendId")]
    public EntitySet<User> Friends
    {
        get { return friends; }
        set { friends.Assign(value); }         
    }
    
    public User(int _id, string _name, string _gender)
    {
        Id = _id;
        Name = _name;
        Gender = _gender;  
        Friends = new EntitySet<User>();
    
    }
    
    [Column(CanBeNull = false)]
    public int MyFriendId { get; set; }//the other user id
    EntityRef<User> myFriend;//the other user object-why??
    [Association(Storage = "myFriend", ThisKey = "MyFriendId", OtherKey = "Id", IsForeignKey = true)]
    public User MyFriend
    {
        get
        { return myFriend.Entity; }
        set
        { myFriend.Entity = value; }
    }
    
    [表格]
    公共类用户
    {
    [列(IsPrimaryKey=true)]
    公共int Id{get;set;}
    [专栏]
    公共字符串名称{get;set;}
    [专栏]
    公共字符串{get;set;}
    private EntitySet friends;//用户知道的用户列表
    [关联(Storage=“friends”,ThisKey=“Id”,OtherKey=“myfriends”)]
    公共实体集好友
    {
    获取{返回朋友;}
    设置{friends.Assign(value);}
    }
    公共用户(整数id、字符串名称、字符串性别)
    {
    Id=_Id;
    名称=_名称;
    性别=_性别;
    Friends=新的EntitySet();
    }
    [列(CanBeNull=false)]
    public int MyFriendId{get;set;}//另一个用户id
    EntityRef myFriend;//另一个用户对象为什么??
    [关联(Storage=“myFriend”,ThisKey=“MyFriendId”,OtherKey=“Id”,IsForeignKey=true)]
    公共用户MyFriend
    {
    得到
    {返回myFriend.Entity;}
    设置
    {myFriend.Entity=value;}
    }
    
    } }

有什么帮助吗?
非常感谢!

我怀疑您是在试图简洁。请添加第二个表来保存相关用户的存储

纵队

身份证, IDOfUserWithRelations& 关联用户ID


QED.

你的意思是我应该有一个表(我们称之为UserRelations),其中有2个用户?(意思是在UserRelations表中,我将有许多行用户a和许多其他用户?)那么它不是多对多吗?是的。它可能看起来像这样:1-USER1-USER2;2-USER1-user3;2-USER1-user4;3-USER2-user5;4-USER2-user6;等等
 [Table]
  public class User
  {
    [Column (IsPrimaryKey=true)]
    public int Id { get; set; }
    [Column]
    public string Name { get; set; }
    [Column]
    public string Gender { get; set; }

private EntitySet<User> friends;//the list of users the user knows
[Association(Storage = "friends", ThisKey = "Id", OtherKey = "MyFriendId")]
public EntitySet<User> Friends
{
    get { return friends; }
    set { friends.Assign(value); }         
}

public User(int _id, string _name, string _gender)
{
    Id = _id;
    Name = _name;
    Gender = _gender;  
    Friends = new EntitySet<User>();

}

[Column(CanBeNull = false)]
public int MyFriendId { get; set; }//the other user id
EntityRef<User> myFriend;//the other user object-why??
[Association(Storage = "myFriend", ThisKey = "MyFriendId", OtherKey = "Id", IsForeignKey = true)]
public User MyFriend
{
    get
    { return myFriend.Entity; }
    set
    { myFriend.Entity = value; }
}