我得到以下異常:避免循環或多個級聯路徑
我經歷了很多帖子here,here和here不見了。但是沒有一篇文章提出了適當的解決方案。我想知道這種情況怎樣才能切實解決。
我Models
和Contexts
如下:
public class Context : DbContext
{
public Context() : base("DefaultConnection")
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
public DbSet<Staff> Staffs { get; set; }
}
public class Student
{
public int StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[Required]
public virtual Course Course { get; set; }
[Required]
public virtual Staff Staff { get; set; }
}
public class Staff
{
public int StaffId { get; set; }
public string Name { get; set; }
public string Contact { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
[Required]
public virtual Staff Staff { get; set; }
}
我正在上線此異常:
context.Students.Add(student);
下面的代碼的
:
public void AddStudent()
{
Student student = new Student();
student.FirstName = "Bruce";
student.LastName = "Wayne";
student.Course = new Course();
student.Course.CourseName = "CSE";
student.Course.Staff = new Staff();
student.Course.Staff.Name = "Albert";
student.Course.Staff.Contact = "1234567890";
context.Students.Add(student);
context.Courses.Add(student.Course);
context.SaveChanges();
Console.WriteLine("Student , Course, Staff Added");
}
希望你的'學生'表看起來像?你有觸發器還是簡單地級聯刪除/更新表? –
@GustavKlimt我首先使用實體框架模型。我的模型如上所述,到目前爲止沒有觸發器 –