【发布时间】:2014-03-28 11:58:53
【问题描述】:
使用 Entity Framework 6,我有一个 Person 类...
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Relationship> Relationships { get; set; }
}
和一个关系类
public class Relationship
{
public int ID { get; set; }
public RelationshipType DependencyType { get; set; }
[ForeignKey("Person")]
public int PersonID { get; set; }
public virtual Person Person { get; set; }
public virtual ICollection<Person> RelatedPersons { get; set; }
}
我希望它表示的是,例如,我可能将 Siblings 作为关系类型,将 UnclesAndAunts 作为另一种关系类型,并持有这些类型的关系,而无需知道父母是谁,因为他们可能不在数据库中.
使用 Code First,这会产生一个表模式......
CREATE TABLE [dbo].[Person](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NULL,
[Relationship_ID] [int] NULL)
和
CREATE TABLE [dbo].[Relationship](
[ID] [int] IDENTITY(1,1) NOT NULL,
[RelationshipType] [int] NOT NULL,
[PersonID] [int] NOT NULL,
[Person_ID] [int] NULL)
PersonID is the main Person of this relationship.
Person_ID is the Person to whom the main person is related to.
I would see the Relationship table containing repeated PersonID data.
问题在于,由于 Person 表中的 Relationship_ID,这意味着 Person 表将有重复的数据,而我希望 Relationship 表有重复的数据,例如
Relationship...
ID RelationshipType PersonID Person_ID
---------------------------------------------
1 Sibling 1 2
2 Sibling 1 3
3 UnclesAndAunts 1 4
谁能告诉我应该如何用模型类来表示它,我假设我需要包含一些属性或其他属性。
谢谢
【问题讨论】:
标签: c# entity-framework model entity relationship