简体   繁体   English

相同类型的多个导航属性的关系配置?

[英]Relashionship configuration of multiple navigation properties of same type?

1) I am trying to establish a relationship between two classes. 1)我正在尝试建立两个班级之间的关系。 So, I have the following class所以,我有以下 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我尝试执行 relashioship 的配置是

   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'.无法在“Team.MatchGames”和“MatchGame.AwayTeam”之间创建关系,因为“Team.MatchGames”和“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'.如果您想在“OnModelCreating”中首先在导航“MatchGame.AwayTeam”上覆盖现有关系调用“Ignore”。

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 EF 代码优先:一对多两次到相同的集合类型

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

EFCore - How to have multiple navigation properties to the same type? EFCore - 如何将多个导航属性设置为同一类型?

2) Also, in order not to create a new post: I wanted to have the public string Team { get; set; } 2)另外,为了不创建新帖子:我想让public string Team { get; set; } public string Team { get; set; } public string Team { get; set; } in the Team Class to be Unique. public string Team { get; set; }在 Class 团队中是唯一的。 I tried some DataAnnotation that I have seen but didn't work.我尝试了一些我见过但没有用的 DataAnnotation。 what do I have to use for this purpose.我必须为此目的使用什么。

The problem is you try to use one navigation property MatchGames for defining relationship.问题是您尝试使用一个导航属性MatchGames来定义关系。 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 :广告 2. 您需要在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.您也可以考虑在 model 等中添加MaxLength属性。

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

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