
課程類別:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Library
{
public class Course
{
public Course()
{
this.Students = new HashSet<Student>();
}
public string CourseId { get; set; }
public string CourseName { get; set; }
public string Department {get; set;}
public virtual ICollection<Student> Students { get; set; }
}
}
學生班
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace Library
{
public class Student
{
public Student()
{
this.Courses = new HashSet<Course>();
}
public string StudentId { get; set; }
[Required]
public string StudentName { get; set; }
public int Age { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
}
我的方法(天藍色函式)
[FunctionName("UpdateCourse")]
public async Task<IActionResult> UpdateCourse(
[HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "courses/{CourseId}")]
HttpRequest req,
ILogger log, string CourseId) {
var course = await _context.Courses.FindAsync(CourseId);
// course.Students.Clear();
foreach (var item in course.Students)
{
course.Students.Remove(item);
}
_context.Update(course);
await _context.SaveChangesAsync();
return new OkObjectResult(removeSelfReference(course));
}
編輯1:
這是我的連接表的代碼優先遷移檔案的一部分CourseStudent
20220415034607_M1.cs:
migrationBuilder.CreateTable(
name: "CourseStudent",
columns: table => new
{
CoursesCourseId = table.Column<string>(type: "nvarchar(450)", nullable: false),
StudentsStudentId = table.Column<string>(type: "nvarchar(450)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_CourseStudent", x => new { x.CoursesCourseId, x.StudentsStudentId });
table.ForeignKey(
name: "FK_CourseStudent_Courses_CoursesCourseId",
column: x => x.CoursesCourseId,
principalTable: "Courses",
principalColumn: "CourseId",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_CourseStudent_Students_StudentsStudentId",
column: x => x.StudentsStudentId,
principalTable: "Students",
principalColumn: "StudentId",
onDelete: ReferentialAction.Cascade);
});
20220415034607_M1.Designer.cs:
modelBuilder.Entity("CourseStudent", b =>
{
b.HasOne("Library.Course", null)
.WithMany()
.HasForeignKey("CoursesCourseId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Library.Student", null)
.WithMany()
.HasForeignKey("StudentsStudentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
uj5u.com熱心網友回復:
您是否確認您的課程延遲加載學生?如果延遲加載被禁用,那么 course.Students 將為空。在修改學生集合時,您應該預先加載它,無論:
var course = context.Courses
.Include(x => x.Students)
.Single(x => x.CourseId == courseId);
從那里你應該能夠使用:
course.Students.Clear();
因為 Course.Students 將是 EF 識別的代理,并且 Clear 方法會將所有條目標記為洗掉。對于多對多關系,您可能需要將課程與每個學生解除關聯:
var course = context.Courses
.Include(x => x.Students)
.Single(x => x.CourseId == courseId);
foreach(var student in course.Students)
student.Courses.Remove(course);
course.Students.Clear();
context.SaveChanges();
以確保在任何學生可能被標記為已修改的情況下洗掉參考。
最后,對于被跟蹤的物體,只需呼叫SaveChanges()而不是呼叫Update。Update用于未跟蹤/分離的物體。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/459595.html
