Я получаю ошибку времени разработки: невозможно создать «DbContext» типа «LeaguesDbContext».C#

Место общения программистов C#
Anonymous
Я получаю ошибку времени разработки: невозможно создать «DbContext» типа «LeaguesDbContext».

Сообщение Anonymous »

Ошибка:

Невозможно создать «DbContext» типа «LeaguesDbContext». Исключение «Произошла ошибка для предупреждения «Microsoft.EntityFrameworkCore.Model.Validation.ForeignKeyPropertiesMappedToUnrelatedTables»: внешний ключ {'LeagueId'} для типа сущности «SoccerDivisionEntity», ориентированного на «SoccerContestLeagueEntity», не может быть представлен в базе данных. Либо свойства {'LeagueId'} не сопоставлены с таблицей "Divisions", либо основные свойства {'Id'} не сопоставлены с таблицей "ContestLeagues". Все свойства внешнего ключа должны сопоставляться с таблицей, с которой сопоставляется зависимый тип, а все основные свойства должны сопоставляться с одной таблицей, с которой сопоставляется основной тип. Это исключение можно подавить или зарегистрировать, передав идентификатор события «RelationalEventId.ForeignKeyPropertiesMappedToUnrelatedTables» методу «ConfigureWarnings» в «DbContext.OnConfiguring» или «AddDbContext». был выброшен при попытке создать экземпляр. Сведения о различных шаблонах, поддерживаемых во время разработки, см. на странице https://go.microsoft.com/fwlink/?linkid=851728

My LeaguesDbContext:

Код: Выделить всё

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Ignore();
modelBuilder.Ignore();
modelBuilder.Ignore();
modelBuilder.Ignore();
modelBuilder.Ignore();
modelBuilder.Ignore();
modelBuilder.Ignore();

// Relationships for Leagues
modelBuilder.Entity()
.HasMany(a => a.Leagues)
.WithOne(l => l.Association)
.HasForeignKey(l => l.AssociationId);

// Relationships for Cups
modelBuilder.Entity()
.HasMany(a => a.Cups)
.WithOne(c => c.Association)
.HasForeignKey(c => c.AssociationId);

modelBuilder.Entity()
.HasMany(d => d.MatchDays)
.WithOne(md => md.Division)
.HasForeignKey(md => md.DivisionId);

modelBuilder.Entity()
.HasOne(d => d.League)
.WithMany(l => l.Divisions)
.HasForeignKey(d => d.LeagueId)
.HasPrincipalKey(l => l.Id);

modelBuilder.Entity()
.Ignore(p => p.PositionRatings);

modelBuilder.Entity()
.Ignore(t => t.StartingLineup)
.Ignore(t => t.Substitutes);

modelBuilder.Entity()
.HasOne(r => r.Cup)
.WithMany(c => c.Rounds)
.HasForeignKey(r => r.CupId);

// Ensure the tables are correctly mapped
modelBuilder.Entity().ToTable("Divisions");
modelBuilder.Entity().ToTable("ContestLeagues");
}

Код: Выделить всё

[Table("Divisions")]
public class SoccerDivisionEntity : SoccerDivision
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; } = Guid.NewGuid();

[ForeignKey("LeagueId")]
public SoccerContestLeagueEntity League { get; set; }
public Guid LeagueId { get; set; }

[Required]
[StringLength(125)]
public string Name { get; set; }

[Required]
public int Level { get; set; } // Lower numbers indicate higher tiers

public virtual ICollection MatchDays { get; set; } = new List();

[InverseProperty("Division")]
public virtual ICollection Teams { get; set; } = new List();

public virtual List Standings { get; set; } = new List();
}

Код: Выделить всё

[Table("ContestLeagues")]
public class SoccerContestLeagueEntity : SoccerContestLeague
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }

[ForeignKey("AssociationId")]
public SoccerAssociationEntity Association { get; set; }
public Guid AssociationId { get; set; }

[InverseProperty("League")]
public ICollection Divisions { get; set; } = new List();

[ForeignKey("ChampionTeamId")]
public SoccerTeamEntity Champion { get; set; }
}

public class SoccerContestLeague : SoccerContest
{
public virtual List Divisions { get; set; } = new List();

public Guid? ChampionTeamId { get; private set; } // Nullable foreign key for champion team
public SoccerTeam Champion { get; private set; }

// Protected constructor for EF Core
protected SoccerContestLeague() { }

//...
}
Что не так?
Я пытался удалить БД во время разработки с помощью PowerShell.

Подробнее здесь: https://stackoverflow.com/questions/788 ... esdbcontex

Вернуться в «C#»