更新时间:2023-02-15 18:47:24
不可能只有一个具有关系的集合.您需要两个-一个与票证等于TicketFrom
的关系,第二个与票证等于TicketTo
的关系.
It's not possible to have just one collection with relations. You need two - one with relations the ticket equals TicketFrom
and second with relations the ticket equals TicketTo
.
类似这样的东西:
型号:
public class Ticket
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Relation> RelatedTo { get; set; }
public virtual ICollection<Relation> RelatedFrom { get; set; }
}
public class Relation
{
public int FromId { get; set; }
public int ToId { get; set; }
public virtual Ticket TicketFrom { get; set; }
public virtual Ticket TicketTo { get; set; }
}
配置:
modelBuilder.Entity<Relation>()
.HasKey(e => new { e.FromId, e.ToId });
modelBuilder.Entity<Relation>()
.HasOne(e => e.TicketFrom)
.WithMany(e => e.RelatedTo)
.HasForeignKey(e => e.FromId);
modelBuilder.Entity<Relation>()
.HasOne(e => e.TicketTo)
.WithMany(e => e.RelatedFrom)
.HasForeignKey(e => e.ToId);
请注意,使用父级的解决方案并不等效,因为它会创建one-to-many
关联,而如果我理解正确,那么您正在寻找many-to-many
.
Note that a solution using Parent is not equivalent, because it would create one-to-many
association, while if I understand correctly you are seeking for many-to-many
.