EF-core 如何设置多对多关系 'itself'
EF-core howto set up many2many relationship to 'itself'
案例:团队成员是在医院工作的任何人。团队成员(医生)可以有其他团队成员作为他们的助手(护士或其他医生),或者团队成员可以是其他团队成员的助手。
类
public class Teammember
{
Public int Id {get; set;}
public ICollection<AssistantLink> AssistantLinks { get; } = new List<AssistantLink>();
public ICollection<Teammember> Assistants => AssistantLinks.Where(x => x.CareProviderId == Id).Select(x => x.Assistant).ToList();
public ICollection<Teammember> CareProviders => AssistantLinks.Where(x => x.AssistantId == Id).Select(x => x.CareProvider).ToList();
}
为了建立这种关系,我创建了一个 class 'AssistantLink'
public class AssistantLink : ModelBase
{
public int CareProviderId { get; set; }
public Teammember CareProvider { get; set; }
public int AssistantId { get; set; }
public Teammember Assistant { get; set; }
}
在数据库上下文中,我试图通过以下方式建立这种关系:
builder.Entity<AssistantLink>().HasKey(x => new { x.CareProviderId, x.AssistantId });
builder.Entity<AssistantLink>().HasOne(x => x.CareProvider).WithMany("AssistantLinks"); //use string notation because navigation property is private
builder.Entity<AssistantLink>().HasOne(x => x.Assistant).WithMany("AssistantLinks");
创建数据库迁移时,我得到以下信息
ERROR : Cannot create a relationship between
'Teammember.AssistantLinks' and 'AssistantLink.Assistant', because
there already is a relationship between 'Teammember.AssistantLinks'
and 'AssistantLink.CareProvider'. Navigation properties can only
participate in a single relationship.
关于如何正确设置它的任何建议?
不可能只有一个集合具有关系。您需要两个 - 一个团队成员的 assistantlinks 等于 CareProvider
,第二个团队成员的 assistantlinks 等于 Assistant
.
型号
public class Teammember
{
public int Id { get; set; }
public ICollection<AssistantLink> Assistants { get; set; }
public ICollection<AssistantLink> Providers { get; set; }
}
public class AssistantLink : ModelBase
{
public int CareProviderId { get; set; }
public Teammember CareProvider { get; set; }
public int AssistantId { get; set; }
public Teammember Assistant { get; set; }
}
配置:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AssistantLink>()
.HasKey(x => new { x.CareProviderId, x.AssistantId });
modelBuilder.Entity<AssistantLink>()
.HasOne(al => al.CareProvider)
.WithMany(c => c.Providers)
.HasForeignKey(al => al.CareProviderId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<AssistantLink>()
.HasOne(al => al.Assistant)
.WithMany(a => a.Assistants)
.HasForeignKey(al => al.AssistantId)
.OnDelete(DeleteBehavior.Restrict);
}
案例:团队成员是在医院工作的任何人。团队成员(医生)可以有其他团队成员作为他们的助手(护士或其他医生),或者团队成员可以是其他团队成员的助手。
类
public class Teammember
{
Public int Id {get; set;}
public ICollection<AssistantLink> AssistantLinks { get; } = new List<AssistantLink>();
public ICollection<Teammember> Assistants => AssistantLinks.Where(x => x.CareProviderId == Id).Select(x => x.Assistant).ToList();
public ICollection<Teammember> CareProviders => AssistantLinks.Where(x => x.AssistantId == Id).Select(x => x.CareProvider).ToList();
}
为了建立这种关系,我创建了一个 class 'AssistantLink'
public class AssistantLink : ModelBase
{
public int CareProviderId { get; set; }
public Teammember CareProvider { get; set; }
public int AssistantId { get; set; }
public Teammember Assistant { get; set; }
}
在数据库上下文中,我试图通过以下方式建立这种关系:
builder.Entity<AssistantLink>().HasKey(x => new { x.CareProviderId, x.AssistantId });
builder.Entity<AssistantLink>().HasOne(x => x.CareProvider).WithMany("AssistantLinks"); //use string notation because navigation property is private
builder.Entity<AssistantLink>().HasOne(x => x.Assistant).WithMany("AssistantLinks");
创建数据库迁移时,我得到以下信息
ERROR : Cannot create a relationship between 'Teammember.AssistantLinks' and 'AssistantLink.Assistant', because there already is a relationship between 'Teammember.AssistantLinks' and 'AssistantLink.CareProvider'. Navigation properties can only participate in a single relationship.
关于如何正确设置它的任何建议?
不可能只有一个集合具有关系。您需要两个 - 一个团队成员的 assistantlinks 等于 CareProvider
,第二个团队成员的 assistantlinks 等于 Assistant
.
型号
public class Teammember
{
public int Id { get; set; }
public ICollection<AssistantLink> Assistants { get; set; }
public ICollection<AssistantLink> Providers { get; set; }
}
public class AssistantLink : ModelBase
{
public int CareProviderId { get; set; }
public Teammember CareProvider { get; set; }
public int AssistantId { get; set; }
public Teammember Assistant { get; set; }
}
配置:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AssistantLink>()
.HasKey(x => new { x.CareProviderId, x.AssistantId });
modelBuilder.Entity<AssistantLink>()
.HasOne(al => al.CareProvider)
.WithMany(c => c.Providers)
.HasForeignKey(al => al.CareProviderId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<AssistantLink>()
.HasOne(al => al.Assistant)
.WithMany(a => a.Assistants)
.HasForeignKey(al => al.AssistantId)
.OnDelete(DeleteBehavior.Restrict);
}