Entity Framework Core 5 引入了多对多关系,而无需直接创建联接表。这很棒并且使编码速度更快,但我遇到了一些挑战。
当处理两个继承同一类(人)的类(学生/教师)之间的关系时,我在迁移后更新数据库时收到错误。
Introducing FOREIGN KEY constraint 'FK_Student_TeacherId' on table 'StudentTeacher' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.
当不使用继承类时,多对多关系似乎可以顺利工作。
public class Person
{
public int Id { get; set; }
public string SchoolName { get; set; }
public int MyProperty { get; set; }
}
public class Teacher : Person
{
public …Run Code Online (Sandbox Code Playgroud) 下面的代码返回ArgumentNullException: Value cannot be null. (Parameter 'source')错误。该错误是由 引发的Model.Players.Any()。看起来页面是在评估隐藏文件的代码之前呈现的。我相信该错误一定与异步调用 OnGetAsync 有关,但我不知道如何修复它。使用 Visual Studio 调试,我已经检查了 Players 属性的值并且计算正确(但是在 razor 页面渲染之后 - 并抛出错误)
文件背后的代码(摘录):
public IEnumerable<Player> Players { get; set; }
public async void OnGetAsync()
{
IEnumerable<Club> clubs = await clubService.GetAllClubs();
IEnumerable<User> users = _userManager.Users;
Players = from user in users
join club in clubs on user.ClubId equals club.ClubId
orderby user.Name
select new Player
{
UserId = new Guid(user.Id),
Name = user.Name,
Surname = user.Surname,
DOB = user.DOB,
ClubId = …Run Code Online (Sandbox Code Playgroud)