[英]How do you create a self-referencing entity in Entity Framework Core?
I have a class, Relationship, that will have an InverseRelationship property. 我有一个类Relationship,它将具有InverseRelationship属性。 Any given Relationship will always have exactly one InverseRelationship which is also a Relationship.
任何给定的关系将始终只有一个InverseRelationship,这也是一个关系。
My class looks like this right now: 我的班级现在看起来像这样:
public class Relationship
{
#region Constructor
public Relationship()
{
}
#endregion Constructor
#region Properties
[Key]
[Required]
public int Id { get; set; }
[Required]
public string Description { get; set; }
public int? InverseRelationshipId { get; set; }
#endregion Properties
#region Related Properties
[ForeignKey("InverseRelationshipId")]
public virtual Relationship InverseRelationship { get; set; }
#endregion Related Properties
}
When I try to populate the table, I get a message saying 当我尝试填充表格时,我收到一条消息,说
Unable to save changes because a circular dependency was detected in the data to be saved
So then I tried to use Fluent API to do this, and I ended up with the following: 因此,我尝试使用Fluent API进行此操作,最终得到以下结果:
modelBuilder.Entity<Relationship>()
.HasOne(r => r.InverseRelationship)
.WithOne(r => r.InverseRelationship)
.HasForeignKey<Relationship>(r => r.InverseRelationshipId)
.IsRequired(false);
And when I try to create a migration, I get the following: 当我尝试创建迁移时,得到以下信息:
The navigation property 'InverseRelationship' cannot be added to the entity type 'Relationship' because a navigation property with the same name already exists on entity type 'Relationship'.
That makes sense, but I don't know how to get around it. 这是有道理的,但我不知道如何解决。 I'm at a loss for how I can achieve what I'm going for here.
我对如何实现这里的目标感到茫然。
Update Here is the code that I used to actually populate the table (prior to trying to use model builder code). 更新这是我用来实际填充表的代码(在尝试使用模型构建器代码之前)。
EntityEntry<Relationship> rel1 = DbContext.Relationships.Add(new Relationship()
{
UserId = authorId,
Title = "Relationship 1",
Description = "Relationship 1",
CreatedDate = createdDate,
LastModifiedDate = lastModifiedDate
});
EntityEntry<Relationship> rel2 = DbContext.Relationships.Add(new Relationship()
{
UserId = authorId,
Title = "Relationship 2",
Description = "Relationship 2",
CreatedDate = createdDate,
LastModifiedDate = lastModifiedDate,
InverseRelationship = rel1.Entity
});
EntityEntry<Relationship> rel3 = DbContext.Relationships.Add(new Relationship()
{
UserId = authorId,
Title = "Relationship 3",
Description = "Relationship 3",
CreatedDate = createdDate,
LastModifiedDate = lastModifiedDate
});
EntityEntry<Relationship> rel4 = DbContext.Relationships.Add(new Relationship()
{
UserId = authorId,
Title = "Relationship 4",
Description = "Relationship 4",
CreatedDate = createdDate,
LastModifiedDate = lastModifiedDate,
InverseRelationship = rel3.Entity
});
rel1.Entity.InverseRelationship = rel2.Entity;
rel3.Entity.InverseRelationship = rel4.Entity;
DbContext.SaveChanges();
Thanks to @Smit, I figured out what was going on. 多亏@Smit,我才知道发生了什么。
Apparently, you need to call 显然,您需要致电
DbContext.SaveChanges();
Before 'closing' the circle in the circular reference. 在“闭合”圆形参考中的圆之前。 Below is working code.
下面是工作代码。
EntityEntry<Relationship> rel1 = DbContext.Relationships.Add(new Relationship()
{
UserId = authorId,
Title = "Relationship 1",
Description = "Relationship 1",
CreatedDate = createdDate,
LastModifiedDate = lastModifiedDate
});
EntityEntry<Relationship> rel2 = DbContext.Relationships.Add(new Relationship()
{
UserId = authorId,
Title = "Relationship 2",
Description = "Relationship 2",
CreatedDate = createdDate,
LastModifiedDate = lastModifiedDate,
InverseRelationship = rel1.Entity
});
EntityEntry<Relationship> rel3 = DbContext.Relationships.Add(new Relationship()
{
UserId = authorId,
Title = "Relationship 3",
Description = "Relationship 3",
CreatedDate = createdDate,
LastModifiedDate = lastModifiedDate
});
EntityEntry<Relationship> rel4 = DbContext.Relationships.Add(new Relationship()
{
UserId = authorId,
Title = "Relationship 4",
Description = "Relationship 4",
CreatedDate = createdDate,
LastModifiedDate = lastModifiedDate,
InverseRelationship = rel3.Entity
});
DbContext.SaveChanges();
rel1.Entity.InverseRelationship = rel2.Entity;
rel3.Entity.InverseRelationship = rel4.Entity;
DbContext.SaveChanges();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.