多次使用导航属性 C# EF Core
Use navigation properties more than once C# EF Core
我有这个数据库结构
public class Gameboard
{
public string Id { get; set; }
public int Round { get; set; }
public int MaxTries { get; set; }
public ICollection<ColorGameBoard> Colors { get; set; }
public ICollection<ColorGameBoard> CorrectColors { get; set; }
}
public class Color
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<ColorGameBoard> ColorGameBoards { get; set; }
}
public class ColorGameBoard
{
public int Id { get; set; }
public int GameBoardId { get; set;}
public Gameboard Gameboard { get; set; }
public int CorrectColorId { get; set; }
public Color CorrectColors { get; set; }
public int ColorId { get; set; }
public Color Color { get; set; }
}
这是我创建关系的方式:
modelBuilder.Entity<ColorGameBoard>()
.HasOne(x => x.Gameboard)
.WithMany(x => x.CorrectColors)
.HasForeignKey(x => x.CorrectColorId);
modelBuilder.Entity<ColorGameBoard>()
.HasOne(x => x.Gameboard)
.WithMany(x => x.Colors)
.HasForeignKey(x => x.ColorId);
modelBuilder.Entity<ColorGameBoard>()
.HasOne(x => x.Color)
.WithMany(x => x.ColorGameBoards)
.HasForeignKey(x => x.GameBoardId);
我收到这个错误:
Cannot create a relationship between 'Gameboard.Colors' and 'ColorGameBoard.Gameboard' because a relationship already exists between 'Gameboard.CorrectColors' and 'ColorGameBoard.Gameboard'. Navigation properties can only participate in a single relationship. If you want to override an existing relationship call 'Ignore' on the navigation 'ColorGameBoard.Gameboard' first in 'OnModelCreating'.
Database structure: On Gameboard has two list with colors. Each list should contain at least 5 colors. And every color in the database can be used on several gameboard. Furthermore, a color can be used as many times a wanted on a gameboard.
有人知道如何修复该错误吗?
您的代码中存在一些对比关系问题。 Gameboardclass中有两个ICollection<ColorGameBoard>
,但ColorGameBoardclass中只有一个 Gameboard
。这样在建库的时候就会出错。比较关系的个数要一致。在 EF Core 中构建 InverseProperty 属性时,可以使用一些属性,例如:virtual
、ForeignKey
、InverseProperty
.
不知道你对数据库的需求是什么,所以我写了两个demo,下面是我的代码:
第一个demo,两个class Gameboard
对应一个class Color
.
Gameboard.class
public class Gameboard
{
public int Id { get; set; }
public int Round { get; set; }
public int MaxTries { get; set; }
[InverseProperty("GameboardOne")]
public ICollection<ColorGameBoard> OneBorad { get; set; }
[InverseProperty("GameboardTwo")]
public ICollection<ColorGameBoard> TwoBoard { get; set; }
}
Color.class
public class Color
{
public int Id { get; set; }
public string Name { get; set; }
[InverseProperty("Color")]
public ICollection<ColorGameBoard> ColorGameBoards { get; set; }
}
ColorGameBoard.class
public class ColorGameBoard
{
public int Id { get; set; }
[ForeignKey("GameboardOne")]
public int OneId { get; set; }
public virtual Gameboard GameboardOne { get; set; }
[ForeignKey("GameboardTwo")]
public int TwoId { get; set; }
public virtual Gameboard GameboardTwo { get; set; }
[ForeignKey("Color")]
public int ColorId { get; set; }
public virtual Color Color { get; set; }
}
模型构建器
modelBuilder.Entity<ColorGameBoard>()
.HasOne(c => c.Color)
.WithMany(a =>a.ColorGameBoards)
.HasForeignKey(b => b.ColorId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<ColorGameBoard>()
.HasOne(d => d.GameboardOne)
.WithMany(e => e.OneBorad)
.HasForeignKey(b => b.OneId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<ColorGameBoard>()
.HasOne(d => d.GameboardTwo)
.WithMany(e => e.TwoBoard)
.HasForeignKey(b => b.TwoId)
.OnDelete(DeleteBehavior.NoAction);
然后,您可以创建数据库
第二个demo,一个class Gameboard
对应两个class Color
.
Gameboard.class
public class Gameboard
{
public int Id { get; set; }
public int Round { get; set; }
public int MaxTries { get; set; }
[InverseProperty("Gameboard")]
public ICollection<ColorGameBoard> Colors { get; set; }
}
Color.class
public class Color
{
public int Id { get; set; }
public string Name { get; set; }
[InverseProperty("Color")]
public ICollection<ColorGameBoard> ColorGameBoards { get; set; }
[InverseProperty("CorrectColors")]
public ICollection<ColorGameBoard> CorrectColors { get; set; }
}
ColorGameBoard.class
public class ColorGameBoard
{
public int Id { get; set; }
[ForeignKey("Gameboard")]
public int GameBoardId { get; set; }
public virtual Gameboard Gameboard { get; set; }
[ForeignKey("CorrectColors")]
public int CorrectColorId { get; set; }
public virtual Color CorrectColors { get; set; }
[ForeignKey("Color")]
public int ColorId { get; set; }
public virtual Color Color { get; set; }
}
模型构建器
modelBuilder.Entity<ColorGameBoard>()
.HasOne(x => x.Gameboard)
.WithMany(x => x.Colors)
.HasForeignKey(x => x.GameBoardId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<ColorGameBoard>()
.HasOne(x => x.CorrectColors)
.WithMany(x => x.CorrectColors)
.HasForeignKey(x => x.CorrectColorId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<ColorGameBoard>()
.HasOne(x => x.Color)
.WithMany(x => x.ColorGameBoards)
.HasForeignKey(x => x.ColorId)
.OnDelete(DeleteBehavior.NoAction);
然后,您可以创建数据库
我有这个数据库结构
public class Gameboard
{
public string Id { get; set; }
public int Round { get; set; }
public int MaxTries { get; set; }
public ICollection<ColorGameBoard> Colors { get; set; }
public ICollection<ColorGameBoard> CorrectColors { get; set; }
}
public class Color
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<ColorGameBoard> ColorGameBoards { get; set; }
}
public class ColorGameBoard
{
public int Id { get; set; }
public int GameBoardId { get; set;}
public Gameboard Gameboard { get; set; }
public int CorrectColorId { get; set; }
public Color CorrectColors { get; set; }
public int ColorId { get; set; }
public Color Color { get; set; }
}
这是我创建关系的方式:
modelBuilder.Entity<ColorGameBoard>()
.HasOne(x => x.Gameboard)
.WithMany(x => x.CorrectColors)
.HasForeignKey(x => x.CorrectColorId);
modelBuilder.Entity<ColorGameBoard>()
.HasOne(x => x.Gameboard)
.WithMany(x => x.Colors)
.HasForeignKey(x => x.ColorId);
modelBuilder.Entity<ColorGameBoard>()
.HasOne(x => x.Color)
.WithMany(x => x.ColorGameBoards)
.HasForeignKey(x => x.GameBoardId);
我收到这个错误:
Cannot create a relationship between 'Gameboard.Colors' and 'ColorGameBoard.Gameboard' because a relationship already exists between 'Gameboard.CorrectColors' and 'ColorGameBoard.Gameboard'. Navigation properties can only participate in a single relationship. If you want to override an existing relationship call 'Ignore' on the navigation 'ColorGameBoard.Gameboard' first in 'OnModelCreating'.
Database structure: On Gameboard has two list with colors. Each list should contain at least 5 colors. And every color in the database can be used on several gameboard. Furthermore, a color can be used as many times a wanted on a gameboard.
有人知道如何修复该错误吗?
您的代码中存在一些对比关系问题。 Gameboardclass中有两个ICollection<ColorGameBoard>
,但ColorGameBoardclass中只有一个 Gameboard
。这样在建库的时候就会出错。比较关系的个数要一致。在 EF Core 中构建 InverseProperty 属性时,可以使用一些属性,例如:virtual
、ForeignKey
、InverseProperty
.
不知道你对数据库的需求是什么,所以我写了两个demo,下面是我的代码:
第一个demo,两个class Gameboard
对应一个class Color
.
Gameboard.class
public class Gameboard
{
public int Id { get; set; }
public int Round { get; set; }
public int MaxTries { get; set; }
[InverseProperty("GameboardOne")]
public ICollection<ColorGameBoard> OneBorad { get; set; }
[InverseProperty("GameboardTwo")]
public ICollection<ColorGameBoard> TwoBoard { get; set; }
}
Color.class
public class Color
{
public int Id { get; set; }
public string Name { get; set; }
[InverseProperty("Color")]
public ICollection<ColorGameBoard> ColorGameBoards { get; set; }
}
ColorGameBoard.class
public class ColorGameBoard
{
public int Id { get; set; }
[ForeignKey("GameboardOne")]
public int OneId { get; set; }
public virtual Gameboard GameboardOne { get; set; }
[ForeignKey("GameboardTwo")]
public int TwoId { get; set; }
public virtual Gameboard GameboardTwo { get; set; }
[ForeignKey("Color")]
public int ColorId { get; set; }
public virtual Color Color { get; set; }
}
模型构建器
modelBuilder.Entity<ColorGameBoard>()
.HasOne(c => c.Color)
.WithMany(a =>a.ColorGameBoards)
.HasForeignKey(b => b.ColorId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<ColorGameBoard>()
.HasOne(d => d.GameboardOne)
.WithMany(e => e.OneBorad)
.HasForeignKey(b => b.OneId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<ColorGameBoard>()
.HasOne(d => d.GameboardTwo)
.WithMany(e => e.TwoBoard)
.HasForeignKey(b => b.TwoId)
.OnDelete(DeleteBehavior.NoAction);
然后,您可以创建数据库
第二个demo,一个class Gameboard
对应两个class Color
.
Gameboard.class
public class Gameboard
{
public int Id { get; set; }
public int Round { get; set; }
public int MaxTries { get; set; }
[InverseProperty("Gameboard")]
public ICollection<ColorGameBoard> Colors { get; set; }
}
Color.class
public class Color
{
public int Id { get; set; }
public string Name { get; set; }
[InverseProperty("Color")]
public ICollection<ColorGameBoard> ColorGameBoards { get; set; }
[InverseProperty("CorrectColors")]
public ICollection<ColorGameBoard> CorrectColors { get; set; }
}
ColorGameBoard.class
public class ColorGameBoard
{
public int Id { get; set; }
[ForeignKey("Gameboard")]
public int GameBoardId { get; set; }
public virtual Gameboard Gameboard { get; set; }
[ForeignKey("CorrectColors")]
public int CorrectColorId { get; set; }
public virtual Color CorrectColors { get; set; }
[ForeignKey("Color")]
public int ColorId { get; set; }
public virtual Color Color { get; set; }
}
模型构建器
modelBuilder.Entity<ColorGameBoard>()
.HasOne(x => x.Gameboard)
.WithMany(x => x.Colors)
.HasForeignKey(x => x.GameBoardId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<ColorGameBoard>()
.HasOne(x => x.CorrectColors)
.WithMany(x => x.CorrectColors)
.HasForeignKey(x => x.CorrectColorId)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<ColorGameBoard>()
.HasOne(x => x.Color)
.WithMany(x => x.ColorGameBoards)
.HasForeignKey(x => x.ColorId)
.OnDelete(DeleteBehavior.NoAction);
然后,您可以创建数据库