3

我正在使用 .NET MVC 4 构建一个网站。我所拥有的是具有与其他人有关的角色的人。例如,A 是 B、​​D 的父亲,A 是 C 的丈夫。

我不确定这是否是正确的方法,但我正在考虑创建一个 Persons 类和一个 Roles 类。不知何故,我想通过 Roles 实例将 Persons 实例与另一个 Persons 实例连接起来,并将其保存在数据库中。

所以关联表必须有三列 Person1|Role|Person2。

我怎样才能最好地在模型中表示这一点?

编辑

谢谢您的帮助。我做了一些与你提议的非常相似的事情。

public class Persons
{
    public Guid PersonsID { get; set; }
    public string Name { get; set; }
    ICollection<Relations> Relations { get; set; } 

}

public class RelationshipType
{
    public Guid ID {get; set;}
    public string Type { get; set; }
    public ICollection<Relations> Relations { get; set; }

}

public class Relations
{
    public Guid RelationsID { get; set; }
    public virtual Persons RealtedPerson { get; set; }
    public virtual RelationshipType RelationType { get; set; }
    public virtual Persons RealtedTo { get; set; }
}

这给出了三个表,其中表 Relations 有两个指向 Persons 的外键和一个指向 RelationshipType 的外键。不,如果我删除一个人,包括此人在内的关系也可能会被删除。

最后一个问题。为什么你选择一个枚举而不是一个导致表格的类?

再次感谢您的帮助。

4

1 回答 1

2

那你应该做这样的事情

class Person{
   string Id{ get; set; }
   string Name{ get; set; }
   ...
   List<Relation> relations{ get; set; }

}

class Relation
{
   string id{ get; set; }
   Person relatedPerson{ get; set; }
   RelationType relationType { get; set; }
   Person relatedto { get; set; }
}

enum RelationType{
  Husband,
  Father,
  Child,
  ...
}
于 2012-12-19T15:03:36.870 回答