简体   繁体   中英

Relashionship configuration of multiple navigation properties of same type?

1) I am trying to establish a relationship between two classes. So, I have the following class

 public class Team
{
    [Key]        
    public int Id { get; set; }

    public string Team { get; set; }

    public List<MatchGame> MatchGames { get; set; }
}

and

public class MatchGame
{
    [Key]
    public int Id { get; set; }

    public int HomeTeamId { get; set; }
    public Team HomeTeam { get; set; }
   
    public int AwayTeamId { get; set; }
    public Team AwayTeam { get; set; }

}

The configuration that I tried to perform the relashioship is

   protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        modelBuilder.Entity<MatchGame>()
            .HasOne(h => h.HomeTeam)
            .WithMany(m => m.MatchGames)
            .HasForeignKey(k => k.HomeTeamId)
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<MatchGame>()
            .HasOne(h => h.AwayTeam)
            .WithMany(m => m.MatchGames)
            .HasForeignKey(k => k.AwayTeamId)
            .OnDelete(DeleteBehavior.NoAction);
    }

the produced error is:

Cannot create a relationship between 'Team.MatchGames' and 'MatchGame.AwayTeam' because a relationship already exists between 'Team.MatchGames' and 'MatchGame.HomeTeam'. Navigation properties can only participate in a single relationship. If you want to override an existing relationship call 'Ignore' on the navigation 'MatchGame.AwayTeam' first in 'OnModelCreating'.

I have also seen the following threads but couldn't find the reason why the relationship couldn't establish.

EF code first: one-to-many twice to same collection type

https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/relationships

EFCore - How to have multiple navigation properties to the same type?

2) Also, in order not to create a new post: I wanted to have the public string Team { get; set; } public string Team { get; set; } public string Team { get; set; } in the Team Class to be Unique. I tried some DataAnnotation that I have seen but didn't work. what do I have to use for this purpose.

The problem is you try to use one navigation property MatchGames for defining relationship. Try create two separate navigation properties like below.

public class Team
{
    public int Id { get; set; }
    public string TeamName { get; set; }
    public List<Match> HomeMatches { get; set; }
    public List<Match> AwayMatches { get; set; }
}

public class MatchGame
{
    public int Id { get; set; }
    public int HomeTeamId { get; set; }
    public Team HomeTeam { get; set; }
    public int AwayTeamId { get; set; }
    public Team AwayTeam { get; set; }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<MatchGame>(entity =>
    {
        entity.HasOne(m => m.HomeTeam)
           .WithMany(t => t.HomeMatches)
           .HasForeignKey(m => m.HomeTeamId)
           .IsRequired()
           .OnDelete(DeleteBehavior.Cascade);

        entity.HasOne(m => m.AwayTeam)
           .WithMany(t => t.AwayMatches)
           .HasForeignKey(m => m.AwayTeamId)
           .IsRequired()
           .OnDelete(DeleteBehavior.Cascade);
    });

}

Ad 2. You need to create unique index on TeamName :

modelBuilder.Entity<Team>(e => e.HasIndex(t => t.TeamName).IsUnique());

Also you can consider to add MaxLength attributes in your model and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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