我得到以下异常:避免循环或多个级联路径
我经历了很多帖子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我首先使用实体框架模型。我的模型如上所述,到目前为止没有触发器 –