Entity Framework 核心一对多
Entity Framework Core One to Many
我正在尝试使用 Entity Framework 核心构建一个 phone 列表。每当我尝试 运行 表扬时:Add-Migration "Initial"
我收到以下错误:Unable to determine the relationship represented by navigation property 'Member.MemberTeam' of type 'Team'. Either manually configure the relationship, or ignore this property from the model.
这是我的模型:
public class Member {
public int Id { get; set; }
public string MemberName { get; set; }
public string MemberTitle { get; set; }
public Member MemberSupervisor { get; set; }
public Team MemberTeam { get; set; }
}
public class Team {
public int Id { get; set; }
public string TeamName { get; set; }
public Member TeamSupervisor { get; set; }
public ICollection<Member> TeamMembers { get; set; }
}
有什么建议吗?
Team
和Member
之间以及Member
和Member
之间存在多种关系。如果你只有这个模型......
public class Member
{
public int Id { get; set; }
public string MemberName { get; set; }
public string MemberTitle { get; set; }
public Team MemberTeam { get; set; }
}
public class Team
{
public int Id { get; set; }
public string TeamName { get; set; }
public ICollection<Member> TeamMembers { get; set; }
}
... EF 不需要任何帮助。可以理解为Team.TeamMembers
和Member.MemberTeam
是一个关联的两端。
现在如果你只添加...
public Member TeamSupervisor { get; set; }
... EF 必须选择这是与 MemberTeam
的一对一关联的反向端,还是除一对多关联之外的新关联。但是,EF 不会为您选择,您必须通过显式映射关联来明确地指示它。例如(在您的 DbContext
子类中):
protected override void OnModelCreating(ModelBuilder mb)
{
mb.Entity<Team>().Property(a => a.Id).UseSqlServerIdentityColumn();
mb.Entity<Team>().HasMany(t => t.TeamMembers).WithOne(m => m.MemberTeam);
mb.Entity<Team>().HasOne(t => t.TeamSupervisor).WithMany()
.HasForeignKey("SupervisorId");
mb.Entity<Member>().Property(a => a.Id).UseSqlServerIdentityColumn();
mb.Entity<Member>().HasOne(m => m.MemberSupervisor).WithMany()
.HasForeignKey("SupervisorId");
}
我正在尝试使用 Entity Framework 核心构建一个 phone 列表。每当我尝试 运行 表扬时:Add-Migration "Initial"
我收到以下错误:Unable to determine the relationship represented by navigation property 'Member.MemberTeam' of type 'Team'. Either manually configure the relationship, or ignore this property from the model.
这是我的模型:
public class Member {
public int Id { get; set; }
public string MemberName { get; set; }
public string MemberTitle { get; set; }
public Member MemberSupervisor { get; set; }
public Team MemberTeam { get; set; }
}
public class Team {
public int Id { get; set; }
public string TeamName { get; set; }
public Member TeamSupervisor { get; set; }
public ICollection<Member> TeamMembers { get; set; }
}
有什么建议吗?
Team
和Member
之间以及Member
和Member
之间存在多种关系。如果你只有这个模型......
public class Member
{
public int Id { get; set; }
public string MemberName { get; set; }
public string MemberTitle { get; set; }
public Team MemberTeam { get; set; }
}
public class Team
{
public int Id { get; set; }
public string TeamName { get; set; }
public ICollection<Member> TeamMembers { get; set; }
}
... EF 不需要任何帮助。可以理解为Team.TeamMembers
和Member.MemberTeam
是一个关联的两端。
现在如果你只添加...
public Member TeamSupervisor { get; set; }
... EF 必须选择这是与 MemberTeam
的一对一关联的反向端,还是除一对多关联之外的新关联。但是,EF 不会为您选择,您必须通过显式映射关联来明确地指示它。例如(在您的 DbContext
子类中):
protected override void OnModelCreating(ModelBuilder mb)
{
mb.Entity<Team>().Property(a => a.Id).UseSqlServerIdentityColumn();
mb.Entity<Team>().HasMany(t => t.TeamMembers).WithOne(m => m.MemberTeam);
mb.Entity<Team>().HasOne(t => t.TeamSupervisor).WithMany()
.HasForeignKey("SupervisorId");
mb.Entity<Member>().Property(a => a.Id).UseSqlServerIdentityColumn();
mb.Entity<Member>().HasOne(m => m.MemberSupervisor).WithMany()
.HasForeignKey("SupervisorId");
}