本文介绍了代码第一实体框架多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以指出我错了什么!我已经创建了两个简单的类,具有多对多的关系。工作正常,所有的表都正确填充。除了当我尝试和撤销任何学生课程没有任何回报...
public partial class Student
{
public Student()
{
课程= new HashSet< Course>();
}
public int StudentID {get;组;
[StringLength(50)]
[必需]
public string FirstName {get; set;}
[StringLength(50)]
[必需]
public string LastName {get; set;}
public DateTime? Enroled {get; set;}
public ICollection< Course>课程{get;组; }
}
public class课程
{
public Course()
{
学生=新的HashSet< Student>();
}
public int CourseID {get;组;
[StringLength(30)]
[必需]
public string CourseTitle {get;组; }
[StringLength(255)]
public string CourseDesc {get;组; }
public ICollection< Student>学生{get;组;
}
public ContextDB()
:base(@Data Source = .\SQLEXPRESS; Initial Catalog = CollegeDB; Integrated Security = True)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Course>()。HasMany(c => c.Students).WithMany(p => p.Courses)
.Map(
m => {
m.MapLeftKey(Course_CourseID);
m.MapRightKey (Student_StudentID);
m.ToTable(CourseStudents);
});
}
public DbSet< Student>学生{get;组; }
public DbSet< Course>课程{get;组; }
}
var students = db.Students.ToList();
foreach(学生在学生中)
{
Console.WriteLine({0} {1},s.FirstName,s.LastName);
foreach(s.Courses.ToList()中的课程c)
{
Console.WriteLine({0},c.CourseTitle);
}
}
解决方案
您正在使用延迟加载,您需要将属性定义为虚拟,
public virtual ICollection< Course>课程{get;组;
如果您希望可以使用热切加载,
var students = db.Students.Include(s => s.Courses).ToList();
Can someone point out where I've got wrong!!
I have created 2 simple classes, with many-to-many relationship. Works fine, all the tables are populated correctly. Except when I try and retrive any Students courses nothing is returned...
public partial class Student
{
public Student()
{
Courses = new HashSet<Course>();
}
public int StudentID { get; set; }
[StringLength(50)]
[Required]
public string FirstName { get; set;}
[StringLength(50)]
[Required]
public string LastName {get; set;}
public DateTime? Enroled {get;set;}
public ICollection<Course> Courses { get; set; }
}
public class Course
{
public Course()
{
Students = new HashSet<Student>();
}
public int CourseID { get; set; }
[StringLength(30)]
[Required]
public string CourseTitle { get; set; }
[StringLength(255)]
public string CourseDesc { get; set; }
public ICollection<Student> Students { get; set; }
}
public ContextDB()
: base (@"Data Source=.\SQLEXPRESS;Initial Catalog=CollegeDB;Integrated Security=True")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>().HasMany(c=>c.Students).WithMany(p => p.Courses)
.Map(
m=> {
m.MapLeftKey("Course_CourseID");
m.MapRightKey("Student_StudentID");
m.ToTable("CourseStudents");
});
}
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
}
var students = db.Students.ToList();
foreach (Student s in students)
{
Console.WriteLine("{0} {1}", s.FirstName, s.LastName);
foreach (Course c in s.Courses.ToList())
{
Console.WriteLine("{0}", c.CourseTitle);
}
}
解决方案
If you are using lazy loading you need to defiine properties as virtual ,
public virtual ICollection<Course> Courses { get; set; }
and if you want you can use eager loading,
var students = db.Students.Include(s=>s.Courses).ToList();
http://codetuner.blogspot.com/2011/07/entity-framework-differed-loading-lazy.html
这篇关于代码第一实体框架多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!