英文:
How to stop creating extra records in joined tables when I create new record in join table in EF Core
问题
我有3个关联表 - 学生,课程和报名。当我尝试使用EF Core创建新的报名时,表会创建带有StudentId和CourseId外键的记录,但也会在学生和课程表中创建额外的记录,而我不希望如此。我知道这是一个相当常见的情况,并且已经查看了几十个回答这个问题的答案 - 但显然我漏掉了一些基本的东西。
这里是3个类的定义:
public class Course
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public List<Enrollment>? Enrollments { get; set; }
}
public class Student
{
public string Id { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public List<Enrollment>? Enrollments { get; set; }
}
public class Enrollment
{
public int Id { get; set; }
public string StudentId { get; set; } = string.Empty;
public virtual Student Student { get; set; } = new Student();
public int CourseId { get; set; }
public virtual Course Course { get; set; } = new Course();
public string Name { get; set; } = string.Empty;
}
我已经在 https://github.com/rswetnam/MultiJoinEF 创建了一个小应用程序,展示了这个问题。对于解决此问题的任何帮助,将不胜感激。
英文:
I have 3 linked tables - Students, Courses and Enrollments. When I try create a new Enrollment with EF Core, the table is created with StudentId and CourseId foreign keys but also extra records in the Student and Courses tables which I do not want. I know this is a fairly common situation and have looked at dozens of answers to this question - but am clearly missing something basic.
Here's the definition of the 3 classes:
public class Course
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public List<Enrollment>? Enrollments { get; set; }
}
public class Student
{
public string Id { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public List<Enrollment>? Enrollments { get; set; }
}
public class Enrollment
{
public int Id { get; set; }
public string StudentId { get; set; } = string.Empty;
public virtual Student Student { get; set; } = new Student();
public int CourseId { get; set; }
public virtual Course Course { get; set; } = new Course();
public string Name { get; set; } = string.Empty;
}
I've created a little app at https://github.com/rswetnam/MultiJoinEF which demonstrates this problem. Any help on fixing this would be greatly appreciated.
答案1
得分: 0
找到Create.cshtml.cs
文件,并将您的代码更改到OnPostAsync
方法中,如下所示,您的问题将得到解决。
// 为了防止 overposting 攻击,请参阅 https://aka.ms/RazorPagesCRUD
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid || _context.Enrollments == null || Enrollment == null)
{
return Page();
}
Course existingCourse = await _context.Courses.FindAsync(Enrollment.CourseId);
Enrollment.Course = existingCourse;
Student existingStudent = await _context.Students.FindAsync(Enrollment.StudentId);
Enrollment.Student = existingStudent;
_context.Enrollments.Add(Enrollment);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
英文:
Find the Create.cshtml.cs
file , and change your code inside the OnPostAsync method like below, and you issue will be fixed.
// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid || _context.Enrollments == null || Enrollment == null)
{
return Page();
}
Course existingCourse = await _context.Courses.FindAsync(Enrollment.CourseId);
Enrollment.Course = existingCourse;
Student existingStudent = await _context.Students.FindAsync(Enrollment.StudentId);
Enrollment.Student = existingStudent;
_context.Enrollments.Add(Enrollment);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论