简体   繁体   English

Entity Framework Core 一对多

[英]Entity Framework Core One to Many

I am trying to build a phone list with Entity Framework Core.我正在尝试使用 Entity Framework Core 构建电话列表。 Whenever I try to run the commend: Add-Migration "Initial"每当我尝试运行推荐时: Add-Migration "Initial"

I get the following error: 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.我收到以下错误: 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. 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.

Here are my models:这是我的模型:

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; }
}

Any advice?有什么建议吗?

There are several relationships between Team and Member and from Member to Member .TeamMember之间以及从MemberMember之间有多种关系。 If you only had this model ...如果你只有这个模型......

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 wouldn't need any help. ... EF 不需要任何帮助。 It would understand that Team.TeamMembers and Member.MemberTeam are two ends of one association.它会理解Team.TeamMembersMember.MemberTeam是一个关联的两端。

Now if you only add ...现在,如果您只添加...

public Member TeamSupervisor { get; set; }

... EF has to choose if this is the reverse end of a one-to-one association with MemberTeam , or a new association besides the one-to-many association. ... EF 必须选择这是与MemberTeam的一对一关联的反向端,还是除MemberTeam关联之外的新关联。 However, EF doesn't choose for you, you have to instruct it unambiguously, by mapping the associations explicitly.但是,EF 不会为您选择,您必须通过显式映射关联来明确指示它。 For example (in your DbContext subclass):例如(在您的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");
}

I know this question is quite old but if you're here this could help you out.我知道这个问题已经很老了,但如果你在这里,这可以帮助你。

In order to solve these relationship issues you can use Has/With pattern .为了解决这些关系问题,您可以使用Has/With 模式

You have two options to face this problem using fluent api.使用 fluent api 有两种选择来解决这个问题。

Option 1: 1 to N选项 1:1 到 N

modelBuilder.Entity<Team>()
  .HasMany(t => t.TeamMembers)
  .WithOne(t => t.MemberTeam);

Option 2: Setting backwards.选项 2:向后设置。

modelBuilder.Entity<Member>()
  .HasOne(t => t.MemberTeam)
  .WithMany(t => t.TeamMembers);

Note: By default the relationship is optional but you can set .IsRequired() if you need it to be present.注意:默认情况下,该关系是可选的,但如果您需要它存在,您可以设置.IsRequired()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM