与实体框架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; }
}
我想要表示的是,例如,我可能将兄弟姐妹作为关系类型,将UnclesAndAunts作为另一种关系类型,并保存这些类型的关系,而无需知道父母是谁,因为他们可能不在数据库中。
随着代码首先,这将产生的表模式...
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.
的问题,这是因为Relationship_ID的Person表中,这是说, Person表将具有重复的数据,而我希望关系表具有重复的数据,例如
Relationship...
ID RelationshipType PersonID Person_ID
---------------------------------------------
1 Sibling 1 2
2 Sibling 1 3
3 UnclesAndAunts 1 4
有人能告诉我应该如何用Model Classes来表示这个,我假设有一些属性或其他需要包含的属性。
感谢
绝对没错,我只是把关系弄错了,最后得到了一个解决方案,与你所建议的一致。唯一的区别是,为了完整性,我添加了第二个ICollection虚拟属性,其InverseProperty为“RelatedPerson”。另外,我没有去Fluent API路由,我只是将RelatedId和RelatedPersonId设置为空(int?),从而解决了级联问题。谢谢。 – Hoots