如何在EF Core中创建新记录时停止在连接表中创建额外记录?

huangapple go评论56阅读模式
英文:

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&lt;Enrollment&gt;? Enrollments { get; set; }
}

public class Student
{
    public string Id { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public List&lt;Enrollment&gt;? 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&lt;IActionResult&gt; 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(&quot;./Index&quot;);
    }

huangapple
  • 本文由 发表于 2023年5月25日 01:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定